Challenge: Creating a Web Server

Learn to create a web server in Node.js by displaying text on the browser using the HTTP module.

Challenge description

In this challenge, we’ll use the HTTP module in Node.js to create a server. We’ll use the createServer() method, which helps us execute the request. The exercise will be implemented by taking the following steps:

  • Step 1: Import the HTTP module.

  • Step 2: Create a variable that holds the server’s functionality using the http.createServer() method.

  • Step 3: Inside the createServer() method, we use the res.writeHead() method to set the content type as text/html, while we use the res.end() method to write HTML using template literal. We’ll create an h2 tag that includes the text “Welcome to the MEAN stack course on Educative.”

  • Step 4: Finally, we’ll chain our function to the listen() method to work on port 5000. We can then start the server with the command below:

node server.js

The expected output should look like the image we have below:

Get hands-on with 1200+ tech skills courses.