Open In App

How to separate routers and controllers in Node.js ?

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the steps to separate routers and controllers in Node.js, which make the overall structure of the application beautiful. It also helps in understanding the code during debugging or updating.

Approach: We will create a simple Node.js application, using Express. This application returns the “Home” string for the main route, “About” for the about the route, and “Contact” for the contact route. Then we will separate the routers and controller into different javascript files.

Implementation: Below is the implementation of the above approach.

 

Creating a simple Node.js application:

Step 1: Initializes NPM: Create and Locate your project folder in the terminal & type the command

npm init -y

It initializes our node application & makes a package.json file.

Step 2: Install Dependencies: Locate your root project directory into the terminal and type the command

npm install express

To install Express as dependencies inside your project

Step 3: Create Server File: Create an ‘app.js’ file, inside this file require an express Module, and create a constant ‘app’ for creating an instance of the express module.

const express = require('express')
const app = express()

Step 4: Creating Routers: Now let’s set up three get routes for ‘/’, ‘/about’, and ‘/contact, inside this route, we will set up a callback function that sends the string, “Home”, “About” and “Contact” respectively.

app.get('/', (req, res) => {
   res.send("Home");
});

app.get('/about', (req, res) => {
   res.send("About");
});

app.get('/contact', (req, res) => {
   res.send("Contact");
});

Step 5: Set up a port to run our server: We will do it by using the express, listen to the method.

app.listen(3000, () => {
    console.log("listening on http://localhost:3000");
})

Complete Code:

app.js




const express = require('express');
const app = express();
  
app.get('/', (req, res) => {
    res.send("Home");
});
  
app.get('/about', (req, res) => {
    res.send("About");
});
  
app.get('/contact', (req, res) => {
    res.send("Contact");
});
  
app.listen(3000, () => {
    console.log("listening on http://localhost:3000");
});


Step to run the application: Inside the terminal type the  following command

node app.js

Output: 

 

Separate Routers:

Step 1: Create a folder for Routers: Let’s create a folder inside the project folder, let’s call it routers, inside this folder, create three files, homerouter.js, aboutrouter.js, and contactrouter.js. 

 

Step 2: Setup Routers files: Inside each file, we have to import the Routers from the express. Then create an instance of that router, we will again call it an app, then copy the respected routers from the app.js to the respective file and simply export it.

homerouter.js




const { Router } = require('express');
const app = Router();
  
app.get('/', (req, res) => {
    res.send("Home");
});
  
module.exports = app;


aboutrouter.js




const { Router } = require('express');
const app = Router();
  
app.get('/about', (req, res) => {
    res.send("About");
});
  
module.exports = app;


contactrouter.js




const { Router } = require('express');
const app = Router();
  
app.get('/contact', (req, res) => {
    res.send("Contact");
});
  
module.exports = app;


Step 3: Import router in app.js: We have to import each router, then we will use, “app.use()” method to use it inside app.js.

app.js




const express = require('express');
const home = require('./routers/homerouter');
const about = require('./routers/aboutrouter');
const contact = require('./routers/contactrouter');
  
const app = express();
  
app.use(home);
app.use(about);
app.use(contact);
  
app.listen(3000, () => {
    console.log("listening on http://localhost:3000");
});


Separate Controllers:

Step 1: Create a folder for Controllers: Let’s create a folder inside the project folder, let’s call it controllers, inside this folder, create three files, homecontroller.js, aboutcontrollerr.js, and contactcontroller.js.

 

Step 2: Setup Controller files: Inside each file, we have to copy the respective controller from the app.js to the respective file and simply export it.

homecontroller.js




module.exports = (req, res) => {
    res.send("Home")
};


aboutcontroller.js




module.exports = (req, res) => {
    res.send("About")
};


contactcontroller.js




module.exports = (req, res) => {
    res.send("Contact")
};


Step 3: Import the controller in each router file respectively, then replace the callback function with it:

const { Router } = require('express');
const home = require('../controllers/homecontroller')

const app = Router();
app.get('/', home);
module.exports = app;

Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads