Adding Routes to Your Server: Node.js

With this video tutorial, lets learn about testing our code snippets using REPL, before implementing it into the actual node application. And then, we’ll show you how to add routes to your node server.

routes-node-server-request-response

In this tutorial, we create 5 routes to the server:
“/” to “Home Page”
“/about” to “About Us”
“/contact” to “Contact Us”
/”satish” redirected to “Home Page”
anything else to “Page Not Found”

Command Prompt
Console Window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\>node
> var url = require("url");
undefined
> var myURL = "http://Technotip.org:3030/about";
undefined
> url
{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] }
> url.parse(myURL).hostname;
'technotip.org'
> url.parse(myURL).port;
'3030'
> url.parse(myURL).pathname;
'/about'
>

Here we test our code snippets on the command prompt, before implementing it into our actual application. Working with node url module on command prompt, we learnt to make use of parse() method of url object to fetch the hostname, port and the pathname of the actual URL. We are interested in pathname – we’ll be using it in our node application.

Related Read: Read Evaluate Print Loop (RELP): Node.js

JavaScript File
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var url= require("url");
var http= require("http");
 
     http.createServer(function(req, res){
 
var pathname= url.parse(req.url).pathname;
 
if( pathname === '/' )
{
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Home Page");
}
else if( pathname === '/about' )
{
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("About Us");
}
else if( pathname === '/contact')
{
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Contact Us");
}
else if( pathname === '/satish' )
{
res.writeHead(301, { "Location": "/" });
res.end("Home Page");
}
else
{
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Page Not Found!!");
}
   }).listen("8080", "127.0.0.1");
 console.log("Server running ..");

Here we require url and http module. Using http object we create HTTP Server. Using url object we call parse method to parse and fetch the path from the user request URL.

Once we fetch the path, using conditional statements we display the corresponding message with appropriate response header information. We also illustrate 301 redirection

Adding Routes to Your Server: Node.js


[youtube https://www.youtube.com/watch?v=ejER-HVhDm8]

YouTube Link: https://www.youtube.com/watch?v=ejER-HVhDm8 [Watch the Video In Full Screen.]



Note: Make sure to turn on the server before trying to access the server!
execution code
command prompt

1
2
3
4
5
 
C:\>cd node
 
C:\node>node app.js
Server running ..

Navigate to the folder where you’ve your app.js file located, and run the server by executing the script using node command.