如何修复“使用Koa在Azure Functions中选择返回承诺或调用'完成'”
我想让koa使用azure函数。 “你好世界”应用程序已经通过koa工作但是在运行dev服务器时,azure会抛出一个错误:
Choose either to return a promise or call 'done'. Do not use both in your script.
使用节点版本10.14.1
代码很简单,你可以看到我从不明确地调用context.done()
。 koa能以某种方式调用此函数吗?由于koa的性质,删除所有承诺不是一种选择。当我尝试调用done时,会出现另一条错误消息:Error: 'done' has already been called. Please check your script for extraneous calls to 'done'.
const Koa = require('koa')
const app = new Koa()
async function createServer(app, context, req){
app.use(async function(ctx) {
ctx.body = 'Hello World';
})
return app.callback()(req, context.res)
}
module.exports = async function (context, req) {
return await createServer(app, context, req)
}
该应用程序正常工作,但我认为它的错误建议忽略错误消息。
回答如下:好吧我明白了。感觉就像一个黑客,但它的工作。
const Koa = require('koa')
const app = new Koa()
async function createServer(app, context, req){
app.use(async function(ctx) {
ctx.body = 'Hello World';
})
//remove done fn from context obj so koa can not call it
context.done = () => {}
return app.callback()(req, context.res)
}
module.exports = async function (context, req) {
return createServer(app, context, req)
}
诀窍是分配qazxsw poi。