Solution Review: Creating a Web Server

Learn to create a web server in Node.js.

We'll cover the following

Explanation

  • We start by requiring the built-in HTTP module in line 1.

  • In line 3, we create a variable called server. This variable holds the logic for the createServer() method that enables us to create an HTTP server that gives a response back to the client. This is made possible by two methods: the res.writeHead() method and the res.end() method.

  • The res.writeHead() method helps us to set the correct content type of ‘text/html’. This allows us to display HTML elements on the page.

  • In line 6, the res.end() method takes in the HTML tags to be displayed on the browser and helps us end the response process.

  • Finally, in line 20, we chain our function using the listen() method to work on port 5000.

Get hands-on with 1400+ tech skills courses.