Part 02_Build a RESTful API using Node.js Express , MongoDB and JWT for authorization for Beginners🚀🚀
By: Renuja De Costa
Tags: Nodejs, Expressjs, Backend, Jwt Token, Rest Api

This is what our REST API is going to look like. You could have an idea of all the routes that are included in the REST API that we are going to include in our API from the above diagram.

localhost:3000/ targets to the root URL localhost:3000/products targets to the products resource and it consists of two types of requests which are GET and POST. The GET request to the products resource returns products and POST request allows to create a products resource. localhost:3000/products/:id targets an individual product and has three types of requests which are GET, PATCH and DELETE.

Lets try to implement this in our code. Before that lets see what we have already on our server.js file.

From what we have right now, we could only target the root URL which is of request type GET .

Now we ‘ll add the GET and POST requests for the /products routes and the /user routes to our code.

server.js file

As you can see the server.js gets a bit messy and becomes even more harder to maintain if we keep on adding all the routes to one file itself. So for simplicity it is much more better to maintain the user routes and product routes on different files separately and makes the code more readable and maintainable rather than packing everything on one file.

folder structure

We’ll be adding all the product routes to the productRoutes.js file and the user routes to the userRoutes.js file.

Before we get into classifying the code in the server.js file into the files productRoutes.js and userRoutes.js , ( although we have discussed about basic routing on the part 1 of this tutorial) we need to know more about routing, writing middleware and how to use middleware.

Middleware

Although we haven’t talked about middleware, we’ve been using it from the get-go of our server.js file.

Middleware is basically anything that happens between the time the server receives the request and sends the response.

Therefore all the app.METHOD functions that we have used with the route handlers are basically middleware functions.

But there’s another way of writing middleware to our application.And that’s by using the app.use() method.

You could read more about middleware on the official express.js website and please do so as we are only covering the basics needed to get the REST API up and running.

So far the middleware we have used have been implemented in an application level i.e. we have created an instance of express by the app object by using app.METHOD() for now and later we’ll also be using app.use()

But we could also implement middleware using Router-level middleware.

Router-level middleware works in the same way as application-level middleware except that it is bounded to an instance of express.Router()

const router = express.Router();

Again , I highly recommend you to read the official documentation for application-level and router-level middleware and the links are given below.

All these will make more sense once we apply these on our REST API. Now lets see how we could use all the theories that we have learnt above.