We’ve already created basic server before so we already know how to start a server and get response. 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 response
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 if else for that and send tell them not found.
Finally you can check using the wrong URL.
So can you write routes now ?
You can see the graphical version here
Originally it published on nerdjfpbblog. You can connect with me in twitter or linkedin !
Top comments (0)