Router, Controller in Express

Tram Ho

Preamble

Hello friends, I have come back, continuing with the basic Nodejs series, today I will introduce you to Express Router and Controller. huh. It is true that the other code is still running, but try to think that you are coding everything in the index.js file and a website needs to manage a lot of things if we throw them all into one file then the management is done. maintain will be very tough. When you subdivide each object to manage, then it is much easier to maintain or develop additional features for the object. Having to edit a file with up to several thousand lines of OMG, it’s already tiring finding and editing in any line: v. Stop rambling, let’s get started

Router

First, I need to create a folder and name it routes to contain the routers that I define, then I create a js file named product.route.js to contain the routers of the product. The .router part is letting me know it’s the router file. If you don’t like it, just leave .js . Inside the product.route.js file, we need to require Express and create a router object from Express . Next, we will copy all the routers from the index.js side and replace the app with the router . Well forget you must exports router file so that we will require it later in the index.js file. Another note is that in both the index.js and product.route.js files, I use the data base connection, so I will separate the part connecting with the databse to a separate file, any file needed will call it. into the. I named it connect.js with the following content.

Next is the product.route.js file

Next, on the index.js file, I create a productRoute require object that product.route.js to

and just call it

My index.js file should look like this

Look more compact than before, right: v

Controller

I have already separated the router file, but in fact, the router file is only used to define the router and the processing logic is separated into a separate file called Controller similar to the MVC model. So I will split the logic on the router side into a new file, the logic here is those functions. Similar to the Router section, we also create a controller folder and a product.controller.js file. Now I need to do in the product.controller.js file, which is exports functions, and the router needs to call it.

For example, the router side has 1 router as follows

Then on the controller side, I will export to a function named index as follows:

Then the router file side just needs to require controller

And the router calls controller.index

Do the same for other routers and controllers. then I have the controller file as follows

Well because it uses the databse connection, you remember to require the conn guy I created above. The router file is as follows

Conclude

So I introduced you to the Router and Controller in Express to make our code somewhat clearer and easier to maintain. The article is somewhat flawed, we hope you to comment below for more, if you find the article useful, give me an upvote, Click follow to be able to follow the latest posts. me please. Thank you for your reading.

Share the news now

Source : Viblo