DEV Community

Bikramjeet Sarmah
Bikramjeet Sarmah

Posted on • Updated on

ECOMMERCE Website Using MERN Part-3.3 ( Product Routes )

In this blog we will be setting the routes for the product and test them using Thunder Client as we are using VS Code, although it's the same as PostMan.

Step-1 Lets setup the base for our route.

1.1 Open up the app.js file and import productRoutes from routes folder after app.use()

1.2 After that we made a base URL for our routes

Here is an example of what am talking about.
Base URL
Here the /api/v1 is the base url as it will be in every request. Ofcourse you can remove it or add another ones also.

So after importing the productRoutes file we write app.use("/api/v1/", productRoutes)

Your app.js file shall look like this

Now lets make the routes in the productRoutes file.

Step-3 Import express and make a constant variable which will have contain Router provided from Express, i.e,

Step-4 Now lets make our first route for Getting All the Products. For that we will be using router.route()
Get all products

As we will be querying only the products so its get request which takes a parameter getAllProducts. Its the exported function which we made in Part-3.2

This function shall auto import but if not just import it by writing the below code

Step-5 Lets make another route for the creating the product. It will have the route product/new and will be a POST request with the createProduct function.
Create Product

Step-6 Before continuing making the rest of the routes lets take a time and test the routes

We will open Thunder Client and lets make a POST request with the following data
POST Request

After sending the response will be

You can also check the data in the database using MongoDb Compass which we have setup in PART-2

The screen shall look like this
Mongodb Compass

Just hit Connect and you will see the databases there

Just click the database and you will see your models, here Product is the model in my ECommerce database.

Your can just click the model and you can see the data in your database

Step-7 Like in Step-5 we will create all the other routes for our product
Other Routes

As all of these Routes will be accessing a param from the url


Here the id is after the /product/.

Here is the Test of the Update Route


Here is the final code of the productRoutes file.

const express = require("express");

const {
} = require("../controllers/productController");

const router = express.Router();


module.exports = router;

Enter fullscreen mode Exit fullscreen mode

Congratulations we have successfully completed making the Product Routes. In the next blog we will be making Error Handler for your Backend

Error Handler for if-else

Top comments (0)