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 thecreateServer()
method that enables us to create an HTTP server that gives a response back to the client. This is made possible by two methods: theres.writeHead()
method and theres.end()
method. -
The
res.writeHead()
method helps us to set the correct content type of ‘text/html’. This allows us to displayHTML
elements on the page. -
In line 6, the
res.end()
method takes in theHTML
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 port5000
.
Get hands-on with 1200+ tech skills courses.