我正在对应用程序进行一些重构,但我不明白为什么这种情况失败了。
index.js
const express = require('express');
const router = express.Router();
const models = require('../models');
const listRoute = require('./list');
router.use('/list', listRoute);
list.js
const express = require('express');
const router = express.Router();
const models = require('../models');
const sendListDataToView = (req, res, view) => {
// get data from backend and pass to template
}
router.route('/list/show/:id')
.get((req, res) => {
sendListDataToView(req, res, 'view-list')
})
router.route('/list/expand-records-forms/:id')
.get((req, res) => {
sendListDataToView(req, res, 'edit-list-records')
})
module.exports = router;
尝试导航到/list/show/3
会引发404错误。但是,如果我将这些路由和sendListDataToView
的定义移至index.js,则页面加载正常。是因为有多个router.route
吗?
答案 0 :(得分:0)
我只是要抹掉原来的答案。
您需要创建该应用。
const express = require('express');
const app = express();
const models = require('../models');
const listRoute = require('./list');
app.use('/list', listRoute); // Using express now.
app.listen(8080); // starts server