Skip to content

The http Module Part -2

The Request Object

Task 1: Create a nodejs server that listens on port "8000", and display "Ok, it works!" in the browser window.

const http = require('http');
const server = http.createServer((request, response) => {
    response.end("Ok, it works!");
});
server.listen(8000);

Task 2: Refer to task 1; log request.url in the console.

const http = require('http');
const server = http.createServer((request, response) => {
    console.log(request.url);
    response.end("Ok, it works!");
});
server.listen(8000);

The result is:

//hit http://localhost:8000/
/
//hit http://localhost:8000/anything
/anything
//hit http://localhost:8000/something/here
/something/here

request.url

The request.url property returns the url portion the user hits in the browser.

Task 3: Refer to task 1; log request.method in the console.

const http = require('http');
const server = http.createServer((request, response) => {
    console.log(request.method);
    response.end("Ok, it works!");
});
server.listen(8000);

The result is:

//hit http://localhost:8000/
GET
//hit http://localhost:8000/anything
GET
//hit http://localhost:8000/something/here
GET

request.method

The request.method property returns the request method. For example, it can be a GET or a POST Method.

Task 4: Create a nodejs server. If the user hits /, display <h1>Welcome to the homepage</h1>. Otherwise; display <h1>Page Not Found</h1>.

const http = require('http');
const server = http.createServer((request, response) => {
    if(request.url === '/'){
        response.end('<h1>Welcome to the homepage</h1>')
    }else{
        response.end("<h1>Page Not Found</h1>");
    }
});
server.listen(8000);

Task 5: Create a nodejs server. If the user hits /, display <h1>Welcome to the homepage</h1>, and if the user hits /about, display <h1>About Page</h1>. Otherwise; display <h1>Page Not Found</h1>.

const http = require('http');
const server = http.createServer((request, response) => {
    if(request.url === '/'){
        response.end('<h1>Welcome to the homepage</h1>')
    }else if(request.url === '/about'){
        response.end('<h1>About Page</h1>');
    }else{
        response.end("<h1>Page Not Found</h1>");
    }
});
server.listen(8000);

response.writeHead()

Task 6: Check the following code. If the user hits /, Hello there is displayed on the browser window. And the response status code is set to 200, and the Content-Type is set to text/plain.

const http = require('http');
const server = http.createServer((request, response) => {
    if(request.url === '/'){
        response.writeHead(200, {"Content-Type": "text/plain"})
        response.end('Hello there')
    }
});
server.listen(8000);

response.writeHead()

The response.writeHead() property enables us to send a response header to the request. For example; we can send an HTTP status code in the response. We also can send the content type of the response.

Task 7: Refer to task 6; if the requested url is other than /, display "Not Found", and set the HTTP status code to 404, and the Content-Type to text/plain.

const http = require('http');
const server = http.createServer((request, response) => {
    if(request.url === '/'){
        response.writeHead(200, {"Content-Type": "text/plain"})
        response.end('Hello there')
    }else{
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.end('Not Found');
    }
});
server.listen(8000);

Mini Projects

Project 1

Task 8: Create a nodejs server that sends the listed responses in the following table:

The requested URL The message to display on the browser HTTP Response Code Content-Type
/ <h1>You have reached the root URL</h1> 200 "text/html"
/admin You have reached the admin area 200 "text/plain"
Anything else Not Found 404 "text/plain"
const http = require('http');
const server = http.createServer((req, res) => {
    if(req.url === "/"){
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end("<h1>You have reached the root URL</h1>");
    }else if(req.url === "/admin"){
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.end("You have reached the admin area");
    }else{
        res.writeHead(404, {"Content-Type": "text/plain"});
        res.end("Not Found");
    }
});
server.listen(8000);

Project 2

Task 9: Create a nodejs server that sends the listed responses in the following table:

The requested URL The message to display on the browser HTTP Response Code Content-Type
/ <h1>Hey welcome here!</h1> 200 "text/html"
/restrictive <h2>You can't get into here</h2> 403 "text/html"
Anything else Not Found 404 "text/plain"
const http = require('http');
const server = http.createServer((req, res) => {
    if(req.url === "/"){
        res.writeHead(200, {"Content-Type": "text/html"});
        res.end("<h1>Hey welcome here!</h1>");
    }else if(req.url === "/restrictive"){
        res.writeHead(403, {"Content-Type": "text/html"});
        res.end("<h2>You can't get into here</h2>");
    }else{
        res.writeHead(404, {"Content-Type": "text/plain"});
        res.end("Not Found");
    }
});
server.listen(8000);