RESTfull routes on Node.js and MongoDB
REST stands for representational state transfer which basically provides a way of mapping HTTP verbs such as (GET, POST, PUT, DELETE) with CRUD actions such as (CREATE, READ, UPDATE, DELETE). Whenever we click on a link on the webpage we are basically making a state transfer from one page to another. For example, when we click for the homepage of the blog website we want to get all the blogs that are stored on the server’s database which is basically a mapping of HTTP GET request with the READ request of database.
Similarly, there are more HTTP requests where we are interacting with the database like creating a blog, editing a blog, deleting a blog, and much more. RESTfull routes are standard conventions of how to implement these mapping. It’s not necessary that you can’t implement such functionality without RESTfull routes but these are the simplest and standard route which are followed by most of the developer. We are going to understand how they work by creating a simple blog application where we can delete, add, create, edit, and display the blog.
|Index||/blogs||GET||List all blog posts|
|New||/blogs/new||GET||Show a form to get blog info|
|Create||/blogs||POST||Add blog to database and redirect to home page|
|Show||/blogs/:id||GET||Show a blog having given id|
|Edit||/blogs/:id/edit||GET||Show edit form for a blog|
|Update||/blogs/:id||PUT||Update a particular blog and redirect to updated blog|
|Destroy||/blogs/:id||DELETE||Delete a particular blog and redirect to home page|
We are going to use node.js which will serve the webpage and MongoDB for storing the data. The entire code is available at Github which includes how to interact with the database and configure all dependency. Just to give little idea about the data schema below is Schema for our blog post.
Note: Entire code is available at Github
Index Page(GET): For the index page, we want to display all the blogs which are stored in the database. To do so first we will get all data from the database using find() function of MongoDB and then send the result to a page that will show those posts. We are finding all the blog posts stored in the database which we are retrieving and handling using the function inside and if an error occurred we are sending to console ERROR! message and if everything works fine then we are rendering the index page where we are passing all blogs as data(blogs) which basically contain all blog info. Which is further used by the index page to display those data.
New Blog(GET) and Create Blog(POST): We want to add a new blog so we need to get the form that will take input from the user. Hence first we will use new.ejs to render the form after that we will fill all the information and after that, we want to save it into the database which we will do by performing a POST request. Observe that, we have the same route as above but here only request type is changed which will be handled separately. We are creating a new blog post by using MongoDB create() function. If we don’t get any error, we will direct to the home page where the user can see that blog has been added and confirms that operation executes successfully.
Show(GET): Generally, the blog home page doesn’t show all blogs entirely it only shows starting few lines and if a user wants to read it based on its title and preview, we want to direct to a new page where the entire blog is shown in our case we will redirect to show.ejs HTML file. To do so all the blogs have unique id associated with it and if we want to display a particular blog, we will simply get the id of that blog and use findById() to get data and send it to show.ejs HTML file where data will be shown.
Edit(GET) and UPDATE(PUT): Sometimes we want to edit a blog, we first need a unique id of the blog after that sent user to a different page where a form is shown which is prefilled with the previous data and the user will update its data after that user will send to updated blog. To do so first get a request to go edit.ejs page then we will use PUT request which will use the findByIdAndUpdate() function of MongoDB to update the data with data that was already in the database under given id.
Destroy(DELETE): Lastly, if we want to delete a specific blog, we will simply use the ID of the blog and use the findByIdAndRemove() function of MongoDB to delete a specific blog from the database.