This tutorial will walk you through the process of creating and running a very basic Node.js server that produces a response when called in a web browser.

Ensure you have followed the tutorial to install Node.js on either your Windows or your Mac machine before starting.

VS Code allows you to create and edit the files needed for your web applications and includes a built-in terminal that allows you to execute the commands needed to run your Node.js server.

Open Visual Studio code.

Under the Terminal menu, select New Terminal.

You will see the command prompt of the Linux operating system. The Terminal is where you will execute commands.

In the Terminal, use the cd and ls commands to navigate to the directory where you will store your project files. You will create a new top-level directory for each project in this course.

Create a new top level directory for our hello-node project and move into that directory.

mkdir hello-node && cd hello-node

You will now create a new file from the Linux command prompt and open it for editing in Visual Studio Code.

Ensure that you are in the directory for our hello-node project and enter the following into the Terminal:

code hellonode.js -r

(Note: the -r may not be necessary on Windows machines, but it will not hurt anything.)

You should now see an empty text file in the Visual Studio Code editor. Save the file (with no content).

Verify that you see the file you just created in the Terminal by typing the following command:

ls

You should see a single file in this directory named hellonode.js.

We will now create a JavaScript file that contains the specifications for a Node.js server. Copy and paste all of the code below into the hellonode.js file. Then, save the file.

//Load HTTP module
const http = require("http");

//Set the IP address and port for this server
const hostname = '127.0.0.1';
const port = 3000;

//Create HTTP server and listen on port 3000 for requests
const server = http.createServer((req, res) => {
  //Set the response HTTP header with HTTP status and Content type
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

//Have the server listen for requests and create a callback function that returns the IP address and port in the server console
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

With your file saved, go to the command line in the Terminal window and type the following command to start your web server:

node hellonode.js

You should see a message in the server console (your Terminal window) that reports the server is running and includes an IP address and port.

You now have a server that is running and waiting for requests. You can make a request of your server by going to a new browser window on the same computer and visit the URL: http://127.0.0.1:3000

This is the IP address and port number that is listening for requests, which you set in your code.

Currently your web server responds with a ‘Hello World' message. Update the hellonode.js file to return a different message of your choosing. Save the file.

Return to the web browser and refresh the page. Did your message change? Why not?

Node.js serves the same code consistently until it is restarted. This prevents inadvertent changes to the application files while the server is up and running. This is also why your message did not change.

In the Terminal in Visual Studio Code, stop your Node.js application by pressing Control-C (which works on both Mac and Windows).

Then, restart your application by typing

node hellonode.js

Return to your web browser and refresh the page. You will now see your new message returned to the browser.