继续获取返回为未定义的 TypeError
2019-08-20
47
我试图在本地启动并运行我的 Node.js 服务器,但它一直返回错误,具体来说:
/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/router/index.js:458
throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a undefined
at Function.use (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/router/index.js:458:13)
at Function.<anonymous> (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/application.js:217:7)
at Object.<anonymous> (/Users/rogerjorns/Desktop/templateassignment/app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:868:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:879:10)
at Module.load (internal/modules/cjs/loader.js:731:32)
at Function.Module._load (internal/modules/cjs/loader.js:644:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:931:10)`
我正在使用 Node.js、Express 和 EJS 模板。 这是我的路由代码:
app.js
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine', 'ejs');
const indexData = require('./routes/index');
const userRoutes = require('./routes/users');
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/index', indexData.routes);
app.use(userRoutes);
app.use((req, res, next) => {
res.status(404).render('404', { pageTitle: 'Page Not Found', path:'/' });
});
app.listen(5000);
path.js
const path = require('path');
module.exports = path.dirname(process.mainModule.filename);
index.js
const path = require('path');
const express = require('express');
const rootDir = require('../util/path');
const indexData = require('./index');
const router = express.Router();
router.get('/', (req, res, next) => {
const users = indexData.users;
res.render('index', {
users: users,
pageTitle: 'Main',
path: '/',
hasUsers: users.length > 0,
activeIndex: true,
indexCSS: true
});
});
// exports.routes = router;
module.exports = router;
users.js
const path = require('path');
const express = require('express');
const rootDir = require('../util/path');
const router = express.Router();
const users = [];
// GET
router.get('/users', (req, res, next) => {
res.render('users', {
pageTitle: 'Users Page',
path: '/index/add-users',
formsCSS: true,
indexCSS: true,
usersCSS: true,
activeUsers: true
});
});
// POST
router.post('/add-users', (req, res, next) => {
users.push({ title: req.body.title });
res.redirect('/');
});
exports.routes = router;
exports.users = users;
它一直出现这个错误。 我已经研究这个问题很长时间了,但对此还是有点陌生。 我尝试过的方法是替换“module.exports = route;”在 index.js 中使用“exports.router;”,这似乎消除了错误,但无论我输入什么,我的页面都会出现 404 错误。 我做错了什么?任何见解都很好。
1个回答
您切换了
indexData
和
userRoutes
:
app.use('/index', indexData.routes);
app.use(userRoutes);
应该是
app.use('/index', indexData);
app.use(userRoutes.routes);
Turtlefight
2019-08-20