node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware
Posted
技术标签:
【中文标题】node_modules/express/lib/router/index.js:458 throw new TypeError(\'Router.use() requires a middleware function but got a \' + gettype(fn))【英文标题】:node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) 【发布时间】:2018-08-11 11:48:31 【问题描述】:我正在尝试创建 nodejs express 应用程序,但在使用路由时遇到了奇怪的问题 这就是我的 server.js 的样子
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const api = require('./server/routes/api');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded(extended:false));
app.use('/api',api);
app.use(express.static(path.join(__dirname,'dist')));
app.get('/',function(req,res)
res.sendFile(path.join(__dirname,'dist/index.html'));
);
const port = process.env.port || 3000;
app.set('port',port);
const server = http.createServer(app);
server.listen(port,function()
console.log('server running at port '+port);
);
我收到以下错误“node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() 需要一个中间件函数但得到一个' + gettype(fn)) "
请告诉我哪里出错了
【问题讨论】:
【参考方案1】:我只是忘记在我的 api.js 中添加 module.exports = router 添加这解决了我的问题
【讨论】:
以上是关于node_modules/express/lib/router/index.js:458 throw new TypeError('Router.use() requires a middleware的主要内容,如果未能解决你的问题,请参考以下文章