gpt4 book ai didi

node.js - Azure 函数 NodeJS : Https Request not working when using Azure Visual Studio Code Extension

转载 作者:太空宇宙 更新时间:2023-11-03 22:05:01 24 4
gpt4 key购买 nike

我正在 NodeJS 中使用 Azure Functions。我首先在我的笔记本电脑上本地编写了具有 azure function 的函数。该代码运行良好并且完成了我想要的一切。现在,我将代码添加到 Azure Visual Studio 代码扩展中的新 azure 函数中 - 完全相同的代码。但现在它不再起作用了。我没有收到错误或其他错误,https 请求只是未启动。

这是我的代码:

const https = require('https');
const fs = require('fs');
const storage = require('azure-storage');
const STORAGE_ACCOUNT_NAME = 'SOMETHING';
const ACCOUNT_ACCESS_KEY = 'SOMETHING';

const blobService = storage.createBlobService(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);

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


https.get(SOMEURL", (resp) => {
let data = '';

// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});

// The whole response has been received. Print out the result.
resp.on('end', async () => {
context.log('here');
});

}).on("error", (err) => {
console.log("Error: " + err.message);
});

};

它永远不会打印应该在 https 请求结束后完成的“here”。 (然而,在这两种情况下都会打印第一个 context.log)

所以我的问题是,我做错了什么?使用 Visual Studio Code 扩展时,我可以不在 azure 函数内使用 https 请求吗?

编辑:

任何需要异步的人,这里有一个链接解释了如何使用 util.promisify 来实现:https://gist.github.com/krnlde/797e5e0a6f12cc9bd563123756fc101f

最佳答案

我保留了您的代码回调。我删除了async定义中的名字并添加了对 context.done 的调用(当您的函数结束时,这会向函数主机发出信号)在 resp.end 中处理程序

const https = require('https');
const fs = require('fs');
const storage = require('azure-storage');
const STORAGE_ACCOUNT_NAME = 'SOMETHING';
const ACCOUNT_ACCESS_KEY = 'SOMETHING';

const blobService = storage.createBlobService(STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY);

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


https.get(SOMEURL", (resp) => {
let data = '';

// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});

// The whole response has been received. Print out the result.
resp.on('end', async () => {
context.log('here');
context.done();
});

}).on("error", (err) => {
console.log("Error: " + err.message);
});

};

另一种选择是将函数保留为 async但您需要将回调替换为 promise基于调用。在某些情况下,这可以通过使用 util.promisify 包装它们来实现。然后用 await 调用他们关键字

关于node.js - Azure 函数 NodeJS : Https Request not working when using Azure Visual Studio Code Extension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56462905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com