发布与expressjs远程URL
我有这个在我的server.js
app.post("/leadAPI/ed",function(request,response){
//api post code here
});
在这篇文章要求我需要张贴包含在请求体到一些外部API与特定的URL数据并发送回响应使用response.send。如何做一个干净的方式。有什么建在这expressjs?
回答如下:作为安德烈亚斯提到的,这是无法表达的义务。其职责是当一个HTTP请求进入调用你的函数。
您可以使用节点内置的HTTP客户端,如安德烈亚斯在评论中也提到,对您的外部网站的请求。
尝试是这样的:
var http = require('http');
app.post("/leadAPI/ed", function(request, response) {
var proxyRequest = http.request({
host: 'remote.site',
port: 80,
method: 'POST',
path: '/endpoint/url'
},
function (proxyResponse) {
proxyResponse.on('data', function (chunk) {
response.send(chunk);
});
});
proxyRequest.write(response.body);
proxyRequest.end();
});
我敢肯定,你需要去适应它来处理分块响应,并找出传输编码的,但是这是你所需要的要点。
有关详细信息,请参阅
http://nodejs/api/http.html