../courses.php
Node
NODE103
90
One Server, Many Requests
Create a small Node server that listens for requests and sends responses.
What does a Node server do?
A Node server waits for incoming requests, decides what each request needs, and sends a response back to the client.
In the last lecture, JavaScript escaped the browser.
Now JavaScript can become the server.
A server does not just print text once and stop. It keeps listening.
Every browser request asks for something: a page, data, an image, a route, or an API response.
Node receives the request and sends back a response.
A tiny Node HTTP server
const http = require("node:http");
const server = http.createServer((request, response) => {
response.statusCode = 200;
response.setHeader("Content-Type", "text/plain");
response.end("Hello from Node server");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
require("node:http") loads Node's built-in HTTP module.
createServer() creates a server that can handle requests.
request contains information about what the client asked for.
response is used to send information back.
listen(3000) starts the server on port 3000.
Request cycle
Servers live on request → response.
APIs
Node can send JSON data to browsers, apps, and other services.
Local testing
A local server lets you test backend behavior on your own machine.
Foundation
Express, routing, APIs, auth, and deployment all build on this request-response idea.
Script that runs once
Server that keeps listening
server.listen(3000, () => {
console.log("Server running");
});
Ask ChatGPT: "Trace this Node server from browser request to server response." Then explain what request and response each do.
Create a file called server.js.
Load the node:http module.
Create a server with createServer().
Send a plain text response.
Run it and visit http://localhost:3000.
Thinking a server script runs once and exits.
Forgetting to call response.end().
Using a port already used by another process.
Confusing the browser request with the server response.
Trying to open the server file directly instead of visiting the local URL.
Create a tiny Node HTTP server, explain request and response, and test the server in a browser.
Login - Jit4All
Login
Welcome back to Jit4All