serverMiddleware does not support async/await with Koa? #8988
Replies: 12 comments
-
Hi @bi-kai You cannot export the const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
const router = new Router({
prefix: '/business'
})
router.get('/article', async (ctx, next) => {
debugger
// await console.log('in server middleware get:/business/article')
await timer()
ctx.response.status = 200
ctx.response.type = 'application/json'
// const data = await { code: 20000, data: { goods: 1 }, message: '' }
ctx.response.body = Object.assign({}, { code: 20000, data: { goods: 2 } })
})
function timer() {
return new Promise(resolve => {
setTimeout(function () {
resolve()
}, 3000)
})
}
app.use(router.routes())
module.exports = app.callback() |
Beta Was this translation helpful? Give feedback.
-
@atinux Hi, I have a index.js to export koa.callback(), I reedited the question's discription. |
Beta Was this translation helpful? Give feedback.
-
Please create a reproduction on CodeSandBox using https://template.nuxtjs.org |
Beta Was this translation helpful? Give feedback.
-
@atinux my question about serverMiddleware is similar to this: https://codesandbox.io/s/vigorous-gauss-eledr?file=/nuxt.config.js |
Beta Was this translation helpful? Give feedback.
-
@bi-kai Actually Nuxt uses |
Beta Was this translation helpful? Give feedback.
-
emm..., Koa/express in serverMiddleware also can't change this behavior, right? so the conclusion is: |
Beta Was this translation helpful? Give feedback.
-
@bi-kai That's not the conclusion I'd come to. Do join the Discord server at https://discord.nuxtjs.org - I'm sure there would be lots of people happy to talk through building API endpoints in serverMiddleware. |
Beta Was this translation helpful? Give feedback.
-
@danielroe no one discuss my quesion on discord, Cloud you help me with this problem? |
Beta Was this translation helpful? Give feedback.
-
Having blocking code is fine. We only have to call {
path: "/test",
handler: async (req, res, next) => {
await new Promise(r =>
setTimeout(() => {
console.log("1");
r();
}, 0)
);
-- await next();
await new Promise(r =>
setTimeout(() => {
console.log("5");
r();
}, 0)
);
++ next();
}
}, |
Beta Was this translation helpful? Give feedback.
-
@pi0 in my example, the code in |
Beta Was this translation helpful? Give feedback.
-
This is how express/connect middleware systam works. They run sequentially and by calling next we go to next middleware we cannot come back. If you prefer splitting logic into multiple middleware: [
async (req, _res, next) => {
req.data = await db.query()
next()
},
(req, res) => {
res.end('Data:' + req.data)
}
] or easier using single middleware to do async operation: async (req, res, _next) => {
const data = await db.query()
res.end('Data:' + data)
} |
Beta Was this translation helpful? Give feedback.
-
@pi0 serverMiddleware is not suitable for koa/express with multi middleware(app.use or router), they all have |
Beta Was this translation helpful? Give feedback.
-
"nuxt": "^2.14.12"
If I use sync code, serverMiddleware runs correctly.
But meets async/await code, koa returns directly in serverMiddleware.
I also test this with koa alone, async/await is supported.
my code is below:
curl http://localhost:3000/api/v1/server-middleware/business/article
The bug is in code:
await timer()
:Koa middleware returns without waitting for 3 seconds, it goes to
next() in router.use()
inserver-middleware/index.js
.The left code in middleware
router.get('/article'
continus running after 3s, but the request has already been returned.Beta Was this translation helpful? Give feedback.
All reactions