Koa - TypeError: Middleware stack must be an array
Koa - TypeError: Middleware stack must be an array
我用 koa-router
创建了路由器:
auth.js
const KoaRouter = require('koa-router');
const { signIn, signUp } = require('../controllers/auth');
const authRouter = new KoaRouter();
authRouter.post('/signin', signIn).post('/signup', signUp);
module.exports = authRouter;
而且我想在我的主应用程序中将它用作中间件。在express中,我可以只写app.use(authRouter)
。但是Koa说中间件应该是一个函数。我找到了一个解决方案,并使用 koa-mount
包来解决这个问题,就像这样 app.use(mount(authRouter))
。但现在它给出了这个错误:
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack
must be an array!')
^
TypeError: Middleware stack must be an array!
但是当我记录 authRouter
时,堆栈 属性 是路由数组:
Router {
opts: {},
methods:
[ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],
params: {},
stack:
[ Layer {
opts: [Object],
name: null,
methods: [Array],
paramNames: [],
stack: [Array],
path: '/signin',
regexp: /^\/signin[\/#\?]?$/i },
Layer {
opts: [Object],
name: null,
methods: [Array],
paramNames: [],
stack: [Array],
path: '/signup',
regexp: /^\/signup[\/#\?]?$/i } ] }
完整代码如下:
const Koa = require('koa');
const KoaRouter = require('koa-router');
const mount = require('koa-mount');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const authRoutes = require('./routes/auth');
const app = new Koa();
const router = new KoaRouter();
console.log(authRoutes);
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
app.use(mount(authRoutes));
app.listen(config.PORT || 3001);
我认为您只是缺少对 authRoutes
对象调用 .routes()
。
const Koa = require('koa');
const KoaRouter = require('koa-router');
const mount = require('koa-mount');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const authRoutes = require('./routes/auth');
const app = new Koa();
const router = new KoaRouter();
console.log(authRoutes);
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
// add this line
app.use(authRoutes.routes()).use(authRoutes.allowedMethods());
app.listen(config.PORT || 3001);
我用 koa-router
创建了路由器:
auth.js
const KoaRouter = require('koa-router');
const { signIn, signUp } = require('../controllers/auth');
const authRouter = new KoaRouter();
authRouter.post('/signin', signIn).post('/signup', signUp);
module.exports = authRouter;
而且我想在我的主应用程序中将它用作中间件。在express中,我可以只写app.use(authRouter)
。但是Koa说中间件应该是一个函数。我找到了一个解决方案,并使用 koa-mount
包来解决这个问题,就像这样 app.use(mount(authRouter))
。但现在它给出了这个错误:
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') ^
TypeError: Middleware stack must be an array!
但是当我记录 authRouter
时,堆栈 属性 是路由数组:
Router {
opts: {},
methods:
[ 'HEAD', 'OPTIONS', 'GET', 'PUT', 'PATCH', 'POST', 'DELETE' ],
params: {},
stack:
[ Layer {
opts: [Object],
name: null,
methods: [Array],
paramNames: [],
stack: [Array],
path: '/signin',
regexp: /^\/signin[\/#\?]?$/i },
Layer {
opts: [Object],
name: null,
methods: [Array],
paramNames: [],
stack: [Array],
path: '/signup',
regexp: /^\/signup[\/#\?]?$/i } ] }
完整代码如下:
const Koa = require('koa');
const KoaRouter = require('koa-router');
const mount = require('koa-mount');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const authRoutes = require('./routes/auth');
const app = new Koa();
const router = new KoaRouter();
console.log(authRoutes);
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
app.use(mount(authRoutes));
app.listen(config.PORT || 3001);
我认为您只是缺少对 authRoutes
对象调用 .routes()
。
const Koa = require('koa');
const KoaRouter = require('koa-router');
const mount = require('koa-mount');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const authRoutes = require('./routes/auth');
const app = new Koa();
const router = new KoaRouter();
console.log(authRoutes);
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());
// add this line
app.use(authRoutes.routes()).use(authRoutes.allowedMethods());
app.listen(config.PORT || 3001);