0

routes.js

var router = require('express').Router();
router.get('/all', require('./all'));
module.exports = router;

all.js work

module.exports = function(req, res, next) {
   res.send('this one work');
};

all.js not work.

const start = function(req, res, next) {
  res.write('start');
  next();
}
const finish = function(req, res, next){
  res.write('finish!');
  res.end();
}

module.exports = function(req, res, next) {
   start,
   finish
};

How to make the all.js to work. Update: complete code of the page

Vuong Tran
  • 31
  • 1
  • 5

2 Answers2

0

You could change your routes.js to something like this:

var router = require('express').Router();
var all = require('./all');
// Call your middlewares in the desired order 
router.get('/all', all.start, all.finish);
module.exports = router;

Read also this question and answers to get some more details.

According to your comment:

all.js:

const start = function(req, res, next) {
  res.write('start');
  next();
}

const finish = function(req, res, next){
  res.write('finish!');
  res.end();
}

module.exports = [start, finish];

routes.js:

var router = require('express').Router();
router.get('/all', require('./all'));
module.exports = router;
pzaenger
  • 11,381
  • 3
  • 45
  • 46
  • Let me know, if I can still improve my answer :) Since you haven't upvoted or accepted answers to any of your questions, please read also [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) :) – pzaenger Oct 22 '18 at 13:53
0

Try this, this will work for you

You have to change only two places in your code otherwise, all the things are right.

routes.js

    var router = require('express').Router();
    var all = require('./all');
    //call both start and finish from all.js file here
    router.get('/all', all.start, all.finish);
    module.exports = router;

all.js

    const start = function (req, res, next) {
        res.write('start');
        next();
    }
    const finish = function (req, res, next) {
        res.write('finish!');
        res.end();
    }

   //export in this way 
    module.exports = {
        start,
        finish
    }