Simple Server: Code-Along
Follow the instructions to create a simple web server using Node.js.
Getting Started
- Create a new Node.js Repl project
- Name it "Simple HTTP Server"
- Create a new file named server.js in the current directory
- In the server.js file, add the line to import the
httpmodule:const http = require('http'); - Under that, add a couple of constants to store the host and port (e.g., where the server connects on the computer)
hostnameshould be'0.0.0.0'andportshould be8080
- Under that, write a message to the console saying "starting up"
- Create a new file named .replit and add
run = "node server.js"to the file - Click the "Run" button to run the program, and make sure it works so far
Code
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
console.log('Starting up...')
Creating the Server
Now, write the code to make a server!
- In the server.js file, define a new function named
handleRequest- This function will be used to respond to any incoming requests from the web browser
- Add two parameters to the
handleRequestfunction:requestandresponse - In the body of the
handleRequestfunction, set thestatusCodeproperty of theresponseobject to200- This means success!
- After setting the status code, call the
endfunction on theresponseobject - Outside of the
handleRequestfunction, usehttp.createServerand pass inhandleRequestto create the server- Store the server in a constant variable named
server
- Store the server in a constant variable named
- Use
server.listento specify the post and hostname (set as constants previously) - Run the program -- a blank webpage should appear!
Code
function handleRequest(request, response) {
response.statusCode = 200;
response.end();
}
const server = http.createServer(handleRequest);
server.listen(port, hostname);
Adding a Callback for Listening
Before continuing, it is important for the server to log its activity to the console.
- Define a new function named
listenCallback - In the body of the function, display a message in the console saying the server is running
- In the
server.listenfunction call, addlistenCallbackas an argument
Code
function listenCallback() {
console.log('Server Running');
}
server.listen(port, hostname, listenCallback);
Creating an HTML Response
While the server is technically functioning, it's not doing much yet. Follow the steps below to make the server actually send some HTML!
- In the body of the
handleRequestfunction, use thesetHeaderfunction of theresponseobject to set the Content Type to HTML - Under that, use
response.writeto write some HTML -<h1>Hello World</h1> - Reload the page to see the HTML!
Obviously, this is a very simple HTML page, but it is simply an example. It is possible to extrapolate this basic structure and create much more elaborate HTML pages.
Code
function handleRequest(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html');
response.write('<h1>Hello World</h1>');
response.end();
}
Using Query Parameters to Change the Response
To make the server a little more dynamic, add the ability to respond to query parameters! The page should say "Hello World" if the user passes along world=1, and "There is no world" for any other value (or no value).
- At the very top of the server.js file, import the
urlmoduleconst url = require('url'); - Find the
handleRequestfunction, and make a new line undersetHeader - There, get the
urlproperty from therequestobject usingrequest.url, and store it in a variable - Under that, parse the URL into an object using the
url.parsefunction- Pass in the request URL and
true, and store the object in a variable
- Pass in the request URL and
- Next, get the
queryproperty of the URL object, and from that, get theworldproperty- Store the result in another variable
- If the world parameter is
1, usereponse.writeto write "Hello World" in an HTML header - If the world parameter is anything else, use
reponse.writeto write "There is no world" in an HTML header - Use
response.end()to end the response
Now it's time to test it out. To do so, open the Repl project website in a new tab by clicking the proper icon in the upper right:

Append ?world=1 to the end of the URL, and go to the page. Verify that the query parameter is working!
Final Code
const http = require('http');
const url = require('url');
const hostname = '0.0.0.0';
const port = 8080;
function handleRequest(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html');
let requestUrl = request.url;
let parsedUrl = url.parse(requestUrl, true);
let queryParams = parsedUrl.query;
let worldParam = queryParams.world;
if (worldParam == 1) {
response.write('<h1>Hello World</h1>');
} else {
response.write('<h1>There is no world</h1>');
}
response.end();
}
const server = http.createServer(handleRequest);
function listenCallback() {
console.log('Server running');
}
server.listen(port, hostname, listenCallback);