Step 7

Let's read the index.html file and send it over through an HTTP response:

const http = require("http");
const fs = require("fs");

const file = fs.readFileSync("./index.html", "utf8");

const server = http.createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "text/html" });
  response.write(file);
  response.end();
});

server.listen(7000);
console.log("Listening! (port 7000)");

Save the file and run it:

node server.js

Open the browser and visit http://localhost:7000/. You should see the following:

A "real" web server usually does more than the one in the example. We'll see more advanced examples in later chapters. For now, we would like to deploy this server, so anyone with an internet connection can access it!

Before moving to the next section, create a GitHub repository and push the code (server.js and index.html) into it.