Thursday, 11 May 2017

Controller with multi action in Express.JS?

I'm making routing by Express. my app load all controllers and each controller has multi action. However, in my controller is Express Router just load default action in my controller and error with all others

Cannot GET /index/action1234


For more specific, this is my code.
The route

app.js

fs.readdirSync('./controllers').forEach(function (file) {
    if(file.substr(-3) == '.js') {
        app.use(require('./controllers/' + file));
    }
});


The controller index.js

var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
    res.send("index/index");
});
router.get('/:action', function(req, res) {
    res.send("index/"+req.params.action);
});
module.exports = router;

Do I have to pass the "app" param to controller and use this instead of router module?
Thanks



via carboncrystal