我正在尝试使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错误。 我究竟做错了什么?这里的任何见解都会很棒。
答案 0 :(得分:1)
您切换了indexData
和userRoutes
:
app.use('/index', indexData.routes);
app.use(userRoutes);
应该是
app.use('/index', indexData);
app.use(userRoutes.routes);