每次运行npm run start
时都会出现此错误。
C:\Users\User\WebDevSensei\Questioner\api\v1\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 Object
at Function.use (C:\Users\User\WebDevSensei\Questioner\api\v1\node_modules\express\lib\router\index.js:458:13)
at Function.<anonymous> (C:\Users\User\WebDevSensei\Questioner\api\v1\node_modules\express\lib\application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (C:\Users\User\WebDevSensei\Questioner\api\v1\node_modules\express\lib\application.js:217:7)
at Object.<anonymous> (C:/Users/User/WebDevSensei/Questioner/api/v1/app.js:17:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at loader (C:\Users\User\WebDevSensei\Questioner\api\v1\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\User\WebDevSensei\Questioner\api\v1\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
[nodemon]应用程序崩溃-等待文件更改,然后再开始...
我刚刚添加了babel来将ES2015编译为ES5。在尝试使用ES6之前,它工作正常,因为这是挑战的要求。
我已经读过the response here的同类错误,但是它不能解决我的问题,因为我的路由文件中有export default router;
。
这是我的 server.js 文件,该文件是我的切入点:
import http from 'http';
import app from './app';
const port = process.env.PORT || 3000;
const server = http.createServer(app);
server.listen(port);
这是我的 app.js 文件:
import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';
const app = express();
const meetupRoutes = require('./routes/meetups');
const questionRoutes = require('./routes/questions');
const usersRoutes = require('./routes/users');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Routes which should handle requests
app.use('api/v1/meetups', meetupRoutes);
app.use('api/v1/questions', questionRoutes);
app.use('api/v1/users', usersRoutes);
// Handling errors generated as a result of unavailable requests
app.use((req, res, next) => {
const error = new Error('Request not found.');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
message: error.message,
});
});
export default app;
此错误可能是什么原因?