Routing: Routing is one of the most significant parts of your website or web application. Routing in Express is basic, adaptable, and robust. Routing is the mechanism by which requests (as specified by a URL and HTTP method) are routed(directed) to the code that handles them.
What is RESTful Routing?
REST stands for Representational State Transfer which provides a way of mapping HTTP verbs (get, post, put, delete) and CRUD actions (create, read, update, delete) together. It is a convention for defining routes and when something follows the rest principle it is known as RESTFUL.
Instead of relying solely on the URL to indicate what site to visit, a RESTful route also depends on the HTTP verb and the URL.
This means that when your application receives an HTTP request, it introspects on that request and identifies the HTTP method and URL, connects that with a corresponding controller action that has that method and URL, executes the code in that action, and determines which response gets sent back to the client.
The internet would be a really confusing place without a convention for how to handle URLs – to delete a Facebook photo might be www.facebook.com/delete-this-photo, but on Instagram, it might be www.instagram.com/remove-this-post. Without a specific convention to follow, it would be hard to create new content, edit content, and delete it. RESTful routes provide a design pattern that allows for easy data manipulation.
For a route to be completely Restful it must do the following:
- Separate the client from the server.
- Not hold state between requests (i.e. all information necessary to respond to a request is available in each request: no data or state, is held by the server from request to request).
- Use HTTP and HTTP methods.
- Be reliable (for e.g most APIs follow the restful route pattern when specifying the process for authentication and important URLs).
There are 7 different restful routes pattern to follow when creating an application or web service that will interact with the server.
Example: For a blog website these Routes would be defined as:
Installing the packages and creating the schema:
npm install express npm install method-override npm install mongoose
The project structure will look like this:
Finally, after creating the routes and adding the styling as per your preference, run this on your command line:
This was a really simple description of what RESTful routing is and what is its practical usage on websites.
- NodeJs - Handling invalid routes
- Nodejs | Automatic restart NodeJs server with nodemon
- Comparison between GraphQL & RESTful Architecture
- Difference between Restless Webservice and Restful Webservice
- HATEOAS and Why It's Needed in RESTful API?
- Routes and Navigator in Flutter
- RESTfull routes on Node.js and MongoDB
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs | Jimp
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.