In the previous chapter, we used NodeJS, as gulp uses it. Node can be used for a server and for a command line tool. In this chapter, we will build a server and in Chapter 9, Playing Tic-Tac-Toe against an AI, we will create a command line application. If you haven't installed Node yet, you can download it from nodejs.org.
We will first create a simple server. We will use Phaethon, a package for Node that makes it easy to build a server in NodeJS. Phaethon includes type definitions, so we can use it immediately. We create a file lib/server/index.ts
and add the following:
import { Server } from "phaethon"; const server = new Server(); server.listener = request => new phaethon.ServerResponse("Hello"); server.listenHttp(8800);
We can run this server using the following command:
gulp && node dist/server
When you open localhost:8800
in a web browser, the listener callback will be called and you will see Hello in the browser.