【发布时间】:2017-05-17 17:41:09
【问题描述】:
我正在尝试使用 index.js 文件将我的路线导出到 express-app 中。
./app.js
const server = require('express'),
app = server(),
port = process.env.PORT
require('./routes/index')(app)
app.listen(port)
./routes/index.js
function routes(app) {
require('./test')(app)
}
module.exports = routes
./routes/test.js
function test(app) {
app.get('/', function (req, res) {
console.info('test')
res.end('Hello, World!')
})
}
module.exports = test
当然,现在,当我执行 ./app.js 时,我收到一条错误消息,告诉我 require( ... ) is not a function on ./routes/index.js:2
但我似乎真的无法将我的大脑包裹在这个逻辑上。不是我错误的原因,而是如何在这个结构中将模块绑定在一起,同时将app 传递给模块。
这种结构背后的想法(如果不是很明显)是创建一个系统,可以通过在属于每个模块的索引文件中添加和删除模块来从应用程序中添加和删除模块,然后只需要每个模块集群的索引(例如路由)。
【问题讨论】:
标签: node.js express module routes