I am starting a series on node js. Actually I’m not super skilled with node js. So I thought let’s start from zero again and help others with me to learn about it. I saw the ‘the net ninja’ tutorials and took the syllabus from there and make a fusion of my own. I want to help you with my journey, let’s ride together into node js for 30 days…
Day 16 — Routing (Basic)— Learning Node JS In 30 Days [Mini series]
A full series to learn the basics easily of node js
We’ve already created basic server before so we already know how to start a server and get reponse. But how we can go on a specific route ?
Solution is not that hard actually. We can check req.url first, which url client is asking for, then we can provide the data basis on that.
To make the routing, first we can check the request url using if and send some reponse based on that.
Run it using node app.js
and check in browser http://localhost:3000/home
Now we can easily add new page. ex- about us page
Stop the server and re-run again using node app.js
We can add more pages like this. But what if someone request a url we didn’t listed ?
Solution is pretty simple just add a else for that and send tell them not found.
Finally you can check using the wrong url.
So can you write routes now ?
Originally it published on nerdjfpb blog and graphical version on instagram. you can connect with me in twitter, linkedin or instagram