1

How do I use multiple router files using express framework?

In my app.js, I have the following code:

var controller = require('./controller/index');
var healthController = require('./controller/health/');

app.use('/', controller);
app.use('/health', healthController);

And controller/index.js:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

module.exports = router;

And health.js:

var express = require('express');
var router = express.Router();

/* GET health confirmation. */
router.get('/health', function(req, res, next) {
  res.send('OK');
});

module.exports = router;

When I hit the http://localhost:8000/, I get the correct page without any problem, however, http://localhost:8000/health results in 404 error.

Thanks in advance.

Srikrishnan Suresh
  • 729
  • 2
  • 13
  • 31
  • 1
    Assuming the "health.js" resides in "controller" directory, may it be just a typo issue? ``var healthController = require('./controller/health/');`` has a trailing slash (/). Removing it would fly? So it becomes ``var healthController = require('./controller/health');`` – tiblu Sep 06 '15 at 00:54
  • @tiblu Can you pls post this as an answer, so that I can accept it for your credit? :-) – Srikrishnan Suresh Sep 06 '15 at 18:00

4 Answers4

2

See How to include route handlers in multiple files in Express?.

Export an anonymous function that can be "initiated" with a reference to the original express app.

./controller/index.js:

module.exports = function(app) {

    /* GET home page. */
    app.get('/', function(req, res, next) {
        res.render('index');
    });
};

./controller/health.js:

module.exports = function(app) {

    /* GET health confirmation. */
    app.get('/health', function(req, res, next) {
        res.send('OK');
    });
};

./app.js:

var app = require('express')();

var controller = require('./controller/index');
var healthController = require('./controller/health');

controller(app);
healthController(app);
Community
  • 1
  • 1
jczimm
  • 360
  • 3
  • 11
2

Your single node app must have single router object, a router object represents a server in express requiring unique port. Hence you should create router object in you app.js passing it to all router files.

Code will be like -

app.js

var express = require('express');

var router = express.Router();

var controller = require('./controller/index');
var healthController = require('./controller/health/');

controller(router);
healthController(router);

index.js

module.exports = function(router) {
    router.get('/', function(req, res, next) {
        res.render('index');
    });
}

health.js

module.exports = funtion(router) {
    router.get('/health', function(req, res, next) {
      res.send('OK');
    });
}
mannuscript
  • 4,711
  • 6
  • 26
  • 25
2

Assuming the "health.js" resides in "controller" directory, may it be just a typo issue? var healthController = require('./controller/health/'); has a trailing slash (/). Removing it would fly? So it becomes var healthController = require('./controller/health');

tiblu
  • 2,959
  • 1
  • 22
  • 22
0

Change in health.js:

router.get('/health', function(req, res, next) {
  res.send('`OK`');
});

to

router.get('/', function(req, res, next) {
  res.send('OK');
});

This will work fine check it out.

Urosh T.
  • 3,336
  • 5
  • 34
  • 42
Ashis
  • 98
  • 1
  • 7