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 theres.writeHead()
method to set the content type astext/html
, while we use theres.end()
method to write HTML using template literal. We’ll create anh2
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 port5000
. 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 1300+ tech skills courses.