我在node.js中使用Express和Consign创建了一个应用程序。通常它适用于本地机器和其他地方,这不是我第一次使用此设置。
这是我用来启动应用程序的代码:
const express = require('express');
const path = require('path');
const consign = require('consign');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const expressSession = require('express-session');
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, './app/views'));
app.use(express.static(path.join(__dirname, './app/public')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.use(expressSession({
secret: 'xxxxxx',
resave: false,
saveUninitialized: false
}));
consign()
.include(path.join('app/routes'))
.then(path.join('app/controllers'))
.into(app);
var port = process.env.PORT || 5000
app.listen(port, function() {
console.log("To app is available in http://localhost:" + port);
});
路由正常工作,当我尝试在一个路由文件中使用时,如下所示:
module.exports = function (application) {
application.get('/SomePathAtAPI', function (req, res) {
application.app.controllers.file.func(application, req, res);
});
}
我有这个错误:
TypeError: Cannot read property 'controllers' of undefined
at /app/app/routes/home.js:5:25
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at next (/app/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at /app/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at session (/app/node_modules/express-session/index.js:454:7)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
at /app/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at /app/node_modules/express-validator/lib/express_validator.js:291:5
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)