Saturday, 27 May 2017

express app handles 404 before forthcoming routes. Not all existed routes are detected by express.Router in time

here is app.js file:

var express = require('express');
var routes = require('./routes/index');
app = express();

app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);  //res.render('404')
});

and routes/index.js:

var express    = require('express')
, router       = express.Router()
var importer = require('./../lib/importer')
    , importRoutes = importer(__dirname)
    , routes = {
        views: importRoutes('./views')
    };

router.get('/login', routes.views.login.get);
router.post('/login', routes.views.login.post);
router.get('/register', routes.views.register.get);
... and many other routes (more than 50)
router.get('/register', routes.views.auth.oneOfTheLastRoutes.get);
module.exports = router;

BUT if I go to localhost:3000/oneOfTheLastRoutes or some route from the bottom the app responses 404, even this route really exist in routes/index.js, and if I move this route higher in the module the app will give correct response.

I'm almost sure that the matter is in lib/importer.js where some async code requires all routes from the routes/views folder, and not all routes are assigned to rotutes.view property.

var fs = require('fs');
var path = require('path');
function dispatchImporter (rel__dirname) {

    function importer (from) {
        //console.log('importing ', from);
        var imported = {};
        var joinPath = function () {
            return '.' + path.sep + path.join.apply(path, arguments);
        };

        var fsPath = joinPath(path.relative(process.cwd(), rel__dirname), from);

        fs.readdirSync(fsPath).forEach(function (name) {
            var info = fs.statSync(path.join(fsPath, name));
            //console.log(name);
            if (info.isDirectory()) {
                imported[name] = importer(joinPath(from, name));
            } else {
                // only import files that we can `require`
                var ext = path.extname(name);
                var base = path.basename(name, ext);
                if (require.extensions[ext]) {
                    imported[base] = require(path.join(rel__dirname, from, name));
                } else {
                    console.log('cannot require ', ext);
                }
            }
        });

        return imported;
    }

    return importer;
}

module.exports = dispatchImporter;

AND I didn't managed to come up with a solution, where all route files would be required and assigned to routes object before the 404 handler.

the structure of files is as follows:

app.js
lib/
   importer.js
routes/
   index.js
   views/
      login.js
      register.js
      etc...



via Nozim

No comments:

Post a Comment