Node.js Nodejs https函数需要等待url json响应,并将其作为在客户端打印的响应传递回调用的express模块
我正在尝试使用nodejs express模块托管一个服务器,当它收到来自客户端的点击时,https模块将尝试点击另一个url并获取json响应保存它,并将此响应作为对其点击的响应提供回客户端。PFB my code可以在服务器端而不是客户端打印响应,PFB my code可以在服务器端而不是客户端打印响应。我尝试了下面的注释方式,但得到了空答案。我打算用卷发去快车Node.js Nodejs https函数需要等待url json响应,并将其作为在客户端打印的响应传递回调用的express模块,node.js,express,https,Node.js,Express,Https,我正在尝试使用nodejs express模块托管一个服务器,当它收到来自客户端的点击时,https模块将尝试点击另一个url并获取json响应保存它,并将此响应作为对其点击的响应提供回客户端。PFB my code可以在服务器端而不是客户端打印响应,PFB my code可以在服务器端而不是客户端打印响应。我尝试了下面的注释方式,但得到了空答案。我打算用卷发去快车 const express = require('express') const app = express() const p
const express = require('express')
const app = express()
const port = 3000
var url = require('url');
var https = require('https');
var HttpsProxyAgent = require('https-proxy-agent')
var responsedata = "";
function delay() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(42);
}, 10000);
});
}
app.get('/', async (request, response) => {
// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://bc-proxy-vip.de.pri.o2.com:8080';
console.log('using proxy server %j', proxy);
// HTTPS endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'https://api.spatialbuzz.com/api/coverage/2010-11-22/near/address/ehrwalder%20str%2064%2081377/customer/CSIEK3QbW2cycVMMWXddBR4xZl5yVA5QAVo/auth/84120D1A::F5305563';
console.log('attempting to GET %j', endpoint);
var options = url.parse(endpoint);
// Creating an instance of the `HttpsProxyAgent` class with the proxy server information
var agent = new HttpsProxyAgent(proxy);
options.agent = agent;
var req = await https.request(options, function(res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
console.log(res.data); // *** null value undefined
res.pipe(process.stdout); // *** Working fine prints json resonse in server side
responsedata = res.data;
///responsedata1 = res.on('data', function (chunk) {
//console.log('BODY: ' + chunk);
//responsedata = ('BODY: ' + chunk);
//responsedata += chunk;
//console.log(responsedata);
//return (responsedata);
});
//console.log(responsedata); // *** Printing null value
// });
// req.on('error', (e) => {
// console.log(`problem with request: ${e.message}`);
//});
req.end();
response.send("Helloworld") // *** WORKING printing helloworld in client side
//response.send('responsedata'); // NOT WORKING printing null repsonse})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})