node.js - 如何使用 Koa 修复 Azure Functions 中的 "Choose either to return a promise or call ' did'"

标签 node.js function azure koa

我正在尝试让 koa 使用 azure 函数。 “hello world”应用程序已经通过 koa 运行,但在运行开发服务器 azure 时抛出错误:

Choose either to return a promise or call 'done'.  Do not use both in your script.

使用 Node 版本10.14.1

代码非常简单,如您所见,我从不显式调用 context.done() 。 koa 能以某种方式调用这个函数吗?由于 koa 的性质,删除所有 promise 并不是一个选择。 当我尝试调用done时,会出现另一条错误消息:错误:'done'已被调用。请检查您的脚本是否存在对“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)
}

技巧是重新分配context.done

关于node.js - 如何使用 Koa 修复 Azure Functions 中的 "Choose either to return a promise or call ' did'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627141/

相关文章:

node.js - 使用 Node.js 和 fetch,当站点始终返回 response.ok = true 时,如何确定站点的 HTTP 版本是否处于事件状态?

javascript - 在不编辑服务器代码的情况下在 node.js 中捕获控制台输出

c - 将二维数组作为参数传递给函数

azure - Azure DSVM 上的 Theano

node.js - 为什么这个 myFunction().then() 操作似乎无限期挂起?

node.js - 502 : Proxy Error from Express app on Ubuntu 18. 04 & Apache2

php - mysql 选择 if 条件

ios - 引用由另一个函数创建的数组

azure - 增加 Azure API 管理中的请求超时

azure - 如何获取 Azure CosmosDb 中的物理分区列表,有没有办法获取物理分区列表?