π― Goal of This Note
Learn how to create a basic web server using Node.js that can accept requests from a browser and send back responses.
π¦ Step 1: Import the http Module in index.js File
const http = require('http');
The http module is another built-in Node.js module (just like
fs).It gives Node networking capabilities β specifically, the ability to build a web server.
Tip: It's helpful to comment out any earlier file-reading/writing code at this point, and organize your file with comments like
// FILESand// SERVERto separate sections clearly.
π οΈ Step 2: Building a Server β Two Main Steps
To create a working server in Node.js, you need to:
Create the server.
Start (listen on) the server so it can accept incoming requests.
π Creating the Server
const server = http.createServer((req, res) => {
res.end('Hello from the server!');
});

π·ββοΈ Building the Server
http.createServer()creates a new server.It accepts a callback function that runs every time a new request hits the server.
This callback receives two important objects:
req(request) β contains information about the incoming request (e.g., the URL requested, headers, etc.)res(response) β provides tools for sending a response back to the client (browser)
π Sending a Response
res.end('Hello from the server!');res.end()is the simplest way to send a plain text response back to whoever made the request.(The reason it's called
.endwill make more sense later β for now, just know it sends and finishes the response.)
Note: Deeper concepts like "requests," "responses," and the client-server model will be explained in a dedicated upcoming lecture on how the web works. For now, the focus is on getting things working practically first, theory later.
π Step 3: Starting the Server (Listening for Requests)
server.listen(8000, '127.0.0.1', () => {
console.log('Listening to requests on port 8000');
});
This method is called on the server variable (the result of createServer) and takes:
πͺ Port number β e.g.,
8000A port is like a sub-address on a specific host/computer.
Common port numbers used in Node development:
8000,3000,80, etc.
π Host (optional)
'127.0.0.1'is the standard IP address for localhost (meaning "this current computer").If not specified, it defaults to
localhostautomatically.
π’ Callback function (optional)
Runs once the server successfully starts listening.
Commonly used to log a confirmation message like:
Listening to requests on port 8000
βΆοΈ Step 4: Running the Server
In the terminal:
node index.js
You'll see the log message:
Listening to requests on port 8000Unlike previous scripts, this program does NOT exit automatically β it keeps running.
This is because the server must stay active to keep listening for new requests.
This behavior relates to something called the event loop, which will be explained in more detail later in the course.
π Step 5: Testing the Server in a Browser
Open a browser.
Go to: (host, followed by a colon
:, followed by the port number)localhost:8000 OR 127.0.0.1:8000You should see the message:
Hello from the server!

π Congratulations β you now have a real, working web server running on your computer using Node.js!
π Step 6: How to Stop the Server
Unlike the REPL (
Ctrl + D), a running Node script is stopped with:
Ctrl + C(Even on Mac β use Control, not Command.)
This is the standard way to stop any Node script from here on out.
π Step 7: Exploring the Request Object
You can inspect the req (request) object to see what data it contains:
const server = http.createServer((req, res) => {
console.log(req);
res.end('Hello from the server!');
});After making changes to code, remember to:
Stop the server (Ctrl + C)
Restart it (
node index.js)Refresh the browser page to trigger a new request
The
reqobject contains a large amount of data, including:HTTP headers (like
host)Many other properties (too much to display fully in the console)
HTTP headers will be explained in more detail in an upcoming topic.
const fs = require('fs');
const http = require('http');
////////////////////////////
//FILES
//Blocking, Synchronous way
//const textIn = fs.readFileSync('./txt/input.txt', 'utf-8');
//console.log(textIn);
//const textOut = `This is what we know about the avocado: ${textIn}.\nCreated on ${Date.now()}`;
//fs.writeFileSync('./txt/output.txt', textOut);
//console.log('File written!');
//Non-blocking, Asynchronous way
// fs.readFile('./txt/start.txt', 'utf8', (err, data1) => {
// fs.readFile(`./txt/${data1}.txt`, 'utf8', (err, data2) => {
// console.log(data2);
// fs.readFile('./txt/append.txt', 'utf8', (err, data3) => {
// console.log(data2, data3);
// fs.writeFile('./txt/final.txt', `${data2}\n${data3}`, 'utf8', (err) => {
// console.log('Your file has been written π');
// });
// });
// });
// });
/////////////////////////////
//SERVER
const server = http.createServer((req, res) => {
console.log(req);
res.end('Hello from the server!');
});
server.listen(8000, '127.0.0.1', () => {
console.log('Listening to requests on port 8000');
});π Recap: What Just Happened
ποΈ We created a server using
http.createServer(), passing in a callback that runs on every incoming request.π We started the server using
server.listen(), specifying the port and host (localhost).π When we visited
localhost:8000in the browser, it triggered a request.β‘ Internally, Node fired an event, which triggered our callback function.
π€ The callback sent back the response:
"Hello from the server!"