0
0
Node.jsframework~30 mins

Creating a basic HTTP server in Node.js - Try It Yourself

Choose your learning style9 modes available
Creating a basic HTTP server
📖 Scenario: You want to create a simple web server that can respond to requests on your computer. This server will listen for visitors and send back a friendly message.
🎯 Goal: Build a basic HTTP server using Node.js that listens on port 3000 and responds with a plain text message.
📋 What You'll Learn
Create an HTTP server using Node.js built-in modules
Set the server to listen on port 3000
Respond with a plain text message 'Hello, world!' to every request
Use proper headers to indicate the content type
💡 Why This Matters
🌍 Real World
Basic HTTP servers are the foundation of web applications and APIs. Knowing how to create one helps you understand how web communication works.
💼 Career
Many backend developer roles require knowledge of creating and managing servers. This skill is essential for building web services and applications.
Progress0 / 4 steps
1
Import the HTTP module
Write a line of code to import the built-in Node.js http module and assign it to a variable called http.
Node.js
Need a hint?

Use require('http') to import the HTTP module.

2
Create the server variable
Create a variable called server and assign it the result of http.createServer() with a function that takes req and res as parameters.
Node.js
Need a hint?

Use http.createServer((req, res) => { }) to create the server.

3
Add response headers and message
Inside the server function, use res.writeHead(200, {'Content-Type': 'text/plain'}) to set the status and headers, then use res.end('Hello, world!') to send the response.
Node.js
Need a hint?

Use res.writeHead to set headers and res.end to send the message.

4
Make the server listen on port 3000
Call server.listen(3000) to make the server listen on port 3000.
Node.js
Need a hint?

Use server.listen(3000) to start the server on port 3000.