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
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 ?