0
0
Node.jsframework~30 mins

What is Node.js in Node.js - Hands-On Activity

Choose your learning style9 modes available
What is Node.js
📖 Scenario: You want to understand what Node.js is and how it works in a simple way.
🎯 Goal: Learn the basic concept of Node.js by creating a simple server that responds with a message.
📋 What You'll Learn
Create a basic Node.js server using the http module
Set up a port number variable
Write the core logic to handle requests and send responses
Start the server and listen on the specified port
💡 Why This Matters
🌍 Real World
Node.js is used to build fast and scalable web servers and applications that run JavaScript outside the browser.
💼 Career
Understanding Node.js basics is essential for backend development roles and full-stack JavaScript development.
Progress0 / 4 steps
1
DATA SETUP: 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 const http = require('http'); to import the module.

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

Use const port = 3000; to set the port.

3
CORE LOGIC: Create the server to handle requests
Use http.createServer with a function that takes req and res as parameters. Inside the function, write the response header with status code 200 and content type text/plain. Then end the response with the text 'Hello from Node.js!'. Assign the server to a constant called server.
Node.js
Need a hint?

Use http.createServer((req, res) => { ... }) and inside write the header and end the response.

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

Use server.listen(port, () => { console.log('Server running on port 3000'); }); to start the server.