Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJS ExpressJS从外部文件路由无效,引发404错误_Javascript_Node.js_Express - Fatal编程技术网

Javascript NodeJS ExpressJS从外部文件路由无效,引发404错误

Javascript NodeJS ExpressJS从外部文件路由无效,引发404错误,javascript,node.js,express,Javascript,Node.js,Express,我正在尝试组织我的导入,并将所有路由放在一个文件中。我似乎无法使它工作。如果您发现代码有任何问题,请告诉我,我愿意接受反馈,因为我刚刚开始学习。我正在寻找最佳实践解决方案或一些常用的、易于维护和开发的解决方案 my app.js express服务器: 'use strict'; const createError = require('http-errors'); const express = require('express'); const path = require('path');

我正在尝试组织我的导入,并将所有路由放在一个文件中。我似乎无法使它工作。如果您发现代码有任何问题,请告诉我,我愿意接受反馈,因为我刚刚开始学习。我正在寻找最佳实践解决方案或一些常用的、易于维护和开发的解决方案

my app.js express服务器:

'use strict';
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const bodyParser = require('body-parser');
const fs = require('fs');
const session = require('express-session');
const redis = require('redis');
const mongoose = require('mongoose');
const redisStore = require('connect-redis')(session);
const client = redis.createClient();

const app = express();

const _UTILS = require('./application/_UTILS');

const db = JSON.parse(fs.readFileSync('/srv/webkb_mean/config/configFiles/database.json', 'utf8'));

mongoose.connect('mongodb://' + db['mongodb']['url'] + '/webKB-main');
mongoose.Promise = global.Promise;

app.use(session({
    secret: _UTILS.getHashedValue(),
    // create new redis store.
    store: new redisStore({
        host: 'localhost',
        port: 6379,
        client: client,
        ttl: 36000
    }),
    saveUninitialized: false,
    resave: false
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({
    extended: false
}));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
    extended: true
}))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'views')));

app.engine('html', require('ejs').renderFile)

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
// app.use(function(err, req, res, next) {
//     // set locals, only providing error in development
//     res.locals.message = err.message;
//     res.locals.error = req.app.get('env') === 'development' ? err : {};
//
//     // render the error page
//     res.status(err.status || 500);
//     res.render('error');
// });
//
require('./config/router')(app);

module.exports = app;
我的路由器文件:

module.exports = (app) => {
    app.use('/', require('./../routes/index'));

    //LOGIN MODULE:
    app.use('/login', require('./../routes/LOGIN/login'));
    app.use('/signin', require('./../routes/LOGIN/signin'));
    app.use('/forgotpassword', require('./../routes/LOGIN/forgotPassword'));
}
堆栈跟踪:

NotFoundError: Not Found
    at /srv/webkb_mean/app.js:57:10
    at Layer.handle [as handle_request] (/srv/webkb_mean/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/srv/webkb_mean/node_modules/express/lib/router/index.js:317:13)
    at /srv/webkb_mean/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/srv/webkb_mean/node_modules/express/lib/router/index.js:335:12)
    at next (/srv/webkb_mean/node_modules/express/lib/router/index.js:275:10)
    at SendStream.error (/srv/webkb_mean/node_modules/serve-static/index.js:121:7)
    at emitOne (events.js:116:13)
    at SendStream.emit (events.js:211:7)
    at SendStream.error (/srv/webkb_mean/node_modules/send/index.js:270:17)
    at SendStream.onStatError (/srv/webkb_mean/node_modules/send/index.js:421:12)
    at next (/srv/webkb_mean/node_modules/send/index.js:764:28)
    at /srv/webkb_mean/node_modules/send/index.js:772:23
    at FSReqWrap.oncomplete (fs.js:152:21)

404中间件总是在路由之前运行。在您在404中间件之前加载路由文件之前,您将获得所有404:-)啊,简单的错误。这很有帮助,谢谢