简体   繁体   English

NodeJS/ExpressJS - 将 http 请求的响应发送回客户端

[英]NodeJS/ExpressJS - Send response of http request back to client

I created a website using Angular 2 and I am using node.js as backend.我使用 Angular 2 创建了一个网站,我使用 node.js 作为后端。

I managed to send a request from the angular client to the node.js server and forwarded this from node.js to another application via an HTTP request.我设法从 angular 客户端向 node.js 服务器发送请求,并通过 HTTP 请求将其从 node.js 转发到另一个应用程序。 My goal is now to send the response from the third application back to the angular client page (I managed also to get the response from the third application), but I am a newbie in node js/express and I couldn't manage to get the response body out of the HTTP request and send it back to the angular client.我现在的目标是将来自第三个应用程序的响应发送回 angular 客户端页面(我也设法从第三个应用程序获取响应),但我是 node js/express 的新手,我无法获得从 HTTP 请求中提取响应正文并将其发送回 angular 客户端。

In the nodejs server I have following code (mostly by looking into tutorials):在 nodejs 服务器中,我有以下代码(主要是通过查看教程):

app.route('/api/test').post((req, res) => {
   postCode(JSON.stringify(req.body));
   //Here I want to send back the response from the third application
   //to the Angular client (instead of 'Hi')
   res.status(200).send('Hi');
});

function postCode(post_data) {

const post_options = {
   host: '127.0.0.1',
   port: '8080',
   path: '/test',
   method: 'POST',
   headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(post_data)
   }
};

// Set up the request
const post_req = http.request(post_options, function (res) {
   res.setEncoding('utf8');
   let responseString = ''
   res.on('data', function (data) {
      responseString += data;
      console.log('Response data: ' + responseString);
   });
   res.on("end", function (data) {
      responseString += data;
      console.log('Response end: ' + responseString);
   });
});

// post the data
post_req.write(post_data);
post_req.end();
}

So, how can I manage now to "use" the response of the HTTP request and send it back in the app.route('/api/arq')?post method to the angular client (Please see also first comment in code)?那么,我现在如何管理“使用”HTTP 请求的响应并将其在 app.route('/api/arq')?post 方法中发送回 angular 客户端(另请参阅代码中的第一条注释) ?

Thank you very much and非常感谢和

Kind regards, Yasemin亲切的问候,亚瑟明

EDIT: seems like this similar pattern was answered already - External API Calls With Express, Node.JS and Require Module thanks to @madflow for finding it.编辑:似乎已经回答了这种类似的模式 - 外部 API 调用与 Express、Node.JS 和 Require 模块感谢@madflow 找到它。

First of all you're doing some extra work in my opinion, why not use a framework like express.js?首先,我认为您正在做一些额外的工作,为什么不使用像 express.js 这样的框架?

Anyhow - what I would do is send the request to your third party from within the /api/arq endpoint, call the http.request after receiving a request inside the route and inside res.on("end"... pass the answer along.无论如何 - 我要做的是从/api/arq端点内向您的第三方发送请求,在收到路由内和res.on("end"...传递答案后调用http.request沿着。

 console.log('Response end: ' + responseString);

The above line is the point where you have the data you want to send in your response.上一行是您拥有要在响应中发送的数据的位置。

So send the response there , and not immediately after you call postCode .所以在那里发送响应,而不是在你调用postCode之后立即postCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM