【问题标题】:How to manage DB driver on Azure Function如何在 Azure Function 上管理 DB 驱动程序
【发布时间】:2020-04-06 04:51:53
【问题描述】:

我想将我在 VM 上的 API 服务器迁移到 Azure 函数。

我在 API 服务器上使用 neo4j-drive。

API 服务器启动时:

const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
try {
    driver.verifyConnectivity()
    console.log('Driver created')
} catch (error) {
    console.log(`connectivity verification failed. ${error}`)
}

在它退出之前:

process.on('exit', function() {
    driver.close();
    console.log('driver closed')
    console.log('exit');
});

process.on('SIGINT', function() {
    console.log('SIGINT');
    process.exit();
});

在 Azure 函数上,我在哪里写它们?

以下是 azure 函数的 hello world 示例:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

【问题讨论】:

    标签: node.js azure neo4j azure-functions


    【解决方案1】:

    你可以把代码放在这前面:

    context.log('JavaScript HTTP trigger function processed a request.');
    

    在那个地方,您可以创建数据库的连接。而且用完之后就可以关闭了。

    module.exports = async function (context, req){}的内容会在满足触发条件时运行。

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 1970-01-01
      • 2021-12-26
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2021-05-23
      相关资源
      最近更新 更多