Creating an HTTP server
Next, we need to set up our project so that it can run ES6 code, specifically the ES6 modules feature. To demonstrate this, and also to show you how to debug your code, we're just going to create a simple HTTP server that always returns the string Hello, World!
.
Note
Normally, when we follow a TDD workflow, we should be writing our tests before we write our application code. However, for the purpose of demonstrating these tools, we will make a small exception here.
Node.js provides the HTTP module, which contains a createServer()
method (https://nodejs.org/api/http.html#http_http_createserver_requestlistener)that allows you to provision HTTP servers. At the root of your project directory, create an index.js
file and add the following:
const http = require('http'); const requestHandler = function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!'); } const server = http.createServer(requestHandler); server.listen(8080);
Note
We are...