In this video we are going to learn about Simple Node Js Routings.
Routing defines the way in which the client requests are handled by the application endpoints.
routes where a user can load different urls in the browser.
Each different url will be a different route,
Let's see how we can set up very basic Node.js routes.
So goto the project and here just create 3 html files.
So just click on new file.
Let say file name is home.html.
Create another file.
Lets say file name is about.html and third file is.
Lets say contact.html.

Now just go to index.html file and write the following code.


<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
</body>
</html>



Now lets open the about.html and write the following code.

<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>HoAboutme</title>
</head>
<body>
<h1>About Page</h1>
</body>
</html>


Now lets open the contact.html and write the following code.


<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>Contact</title>
</head>
<body>
<h1>Contact Page</h1>
</body>
</html>


Now goto the index.php file and require the http and write the following code.


var http = require('http');
var server = http.createServer(function(req,res)
{
    if(req.url === '/home' || req.url === '/')
    {
        res.writeHead(200,{'Content-Type':'text/html'});
        res.createReadStream(__dirname + '/home.html')pipe(res);
    }
    else if(req.url === '/about')
    {
        res.writeHead(200,{'Content-Type':'text/html'});
        res.createReadStream(__dirname + '/about.html')pipe(res);
    }
    else if(req.url === '/contact')
    {
        res.writeHead(200,{'Content-Type':'text/html'});
        res.createReadStream(__dirname + '/about.html')pipe(res);
    }
    else.
    {
        res.writeHead(404,{'Content-Type':'text/html'});
        res.createReadStream(__dirname + '/about.html')pipe(res);
    }
});


Now lets check it.
So go to command prompt and run the index.js file.

node index.js


Now switch to the browser and refresh the page.
You can see here the home page.
If I go to the url /about you will get the about page.
Now if I go to the url /contact you will get the the contact page.
So in this way you can create simple routes.