[Node.js 101] 3. URL based REQUESTS

R1CH4RD5
3 min readMar 24, 2021

--

Keeping in mind that Node.js is a JavaScript code interpreter that works on the server side, Node.js allow us to create easily a HTTP server and respond to requests based on the URL.

To testing this feature, we will considering a store that have four categories like Store, Laptops, Smartphones and Tables, where we will implement a service that responds to requests based on the URL.

[ Developing ]

As done in the previously articles, in a Microsoft Windows 7 machine, and in any location desired (for example, Desktop), press SHIFT+RIGHT MOUSE and select “Open Command Window here”.

(For Windows 10 users if not active, that feature can be activated with some additional steps, where in the future will be a article explaining how to do it but for now you can run the command prompt normally and navigating forward using ‘cd [foldername]’ or backwards using ‘cd..’ until you reach the desired location.)

With the Command Prompt Windows let’s create a new folder for the project named nodejs-urlbasedrequest using the following command:

mkdir nodejs-urlbasedrequest

Define the nodejs-urlbasedrequest as the actual path of the Command Prompt Window navigating into it:

cd nodejs-urlbasedrequest

As showed and explained in the [Node.js 101] 2. Making a Small Weather App, we will initialize the project using a command that is a step-by-step tool to scaffold out the project. This command will prompt you for input for a few aspects of the project. For a simplicity way, you can only input a description and the author if you want. To continue, execute the following command and when asked input the information and/or press ENTER:

npm init

After concluding those steps, we will create a index.js file:

nul type >> index.js

After this processing it’s done, you will have something similar as:

Now let’s edit the index.js using any text or source code editor and put the following code:

var http = require('http');    
var server = http.createServer(function(req, res) {
var cat = req.url;
if (cat == "/laptops"){
res.end('Laptop Page');
console.log (" >Laptop Page Visited");
}else if(cat == "/smartphones") {
res.end('Smartphones Page');
console.log (" >Smartphones Page Visited");
}else if(cat == "/tablets") {
res.end('Tablets Page');
console.log (" >Tablets Page Visited");
}else if(cat == "/") {
res.end("Store Page")
console.log (" >Store Site Visited");
}}).listen(8080,function(){
console.log("Server running at http://localhost:8080");
});

Save it and in the Command Prompt Window execute:

node index.js

If everything is working until this point, the Command Prompt Window will present a message (that was been coded) that the server is running.

If we open a browser and typing the respective link, we can navigating between the different existing pages using the different existing URL’s. Notice that, as been coded, the command prompt window show us the different requests that was been made:

To shutdown the server, on the command prompt window, press CTRL+C where the server will no more be available.

Hope you liked this small article, best regards, Ricardo Costa (Richards).

This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.

--

--

No responses yet