简体   繁体   English

即将到来的路线之前,快速应用处理404。 express.Router不能及时检测到所有现有路由

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

here is app.js file: 这是app.js文件:

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: 和route / 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('/theLastRoute', 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. 但是,如果我转到localhost:3000/oneOfTheLastRoutes或应用程序响应的底部路由404,即使此路由确实存在于route / index.js中,并且如果我将此路由移至模块中的较高位置,应用程序将给出正确的响应。

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. 我几乎可以确定问题出在lib / importer.js中,其中一些异步代码需要来自route / views文件夹的所有路由,而并非所有路由都分配给rotutes.view属性。

importer.js: importer.js:

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. 而且我没有设法提出一个解决方案,在该解决方案中,将需要所有路由文件并将其分配给404处理程序之前的routes对象。

the structure of files is as follows: 文件的结构如下:

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

问题出在其他模块中,在该模块中声明了重叠的路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM