0
0
Node.jsframework~15 mins

Why Node.js for server-side JavaScript in Node.js - See It in Action

Choose your learning style9 modes available
Why Node.js for server-side JavaScript
📖 Scenario: You are building a simple server that responds to web requests. You want to understand why Node.js is a good choice for running JavaScript on the server.
🎯 Goal: Create a basic Node.js server script step-by-step to see how Node.js handles server-side JavaScript efficiently.
📋 What You'll Learn
Create a simple HTTP server using Node.js built-in modules
Set a port number for the server to listen on
Write the core logic to respond with a friendly message
Complete the server setup to start listening on the port
💡 Why This Matters
🌍 Real World
Node.js is widely used to build fast and scalable web servers that handle many users at once without slowing down.
💼 Career
Understanding how to create a basic Node.js server is a key skill for backend developers working with JavaScript.
Progress0 / 4 steps
1
DATA SETUP: Import the HTTP module
Write a line to import the built-in Node.js http module using import syntax.
Node.js
Need a hint?

Use import http from 'http'; to bring in the HTTP module.

2
CONFIGURATION: Set the server port
Create a constant called port and set it to 3000.
Node.js
Need a hint?

Use const port = 3000; to define the port.

3
CORE LOGIC: Create the server with a response
Use http.createServer with a function that takes req and res. Inside, write headers with res.writeHead(200, {'Content-Type': 'text/plain'}) and send the text 'Hello from Node.js!' with res.end(). Assign this server to a constant called server.
Node.js
Need a hint?

Use http.createServer and respond with a plain text message.

4
COMPLETION: Start the server listening on the port
Call server.listen with the port constant and add a callback function that logs 'Server running on port 3000'.
Node.js
Need a hint?

Use server.listen(port, () => { console.log(...) }) to start the server.