简体   繁体   English

Node.js-服务来自同一服务器上运行的tomcat的请求

[英]Nodejs - serve a request from tomcat running on same server

I have a nodejs server running on port 3000 works fine. 我有一个在端口3000上运行的nodejs服务器运行正常。

I need to make a request to a tomcat server running on the same server, on port 8080 and return the header and body response to the client, basically hiding the fact that the tomcat server is running on another port from the client. 我需要向运行在同一服务器上端口8080上的tomcat服务器发出请求,并将标头和正文响应返回给客户端,这基本上隐藏了tomcat服务器正在从客户端的另一个端口上运行的事实。

var request = require('request');
var url = "http://localhost:8080/?somevar=someval"
request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        res.send(body);
    }
})

This of course returns the returns the body of the response from tomcat, but not the header. 当然,这会返回tomcat的响应正文,但不会返回标头。

Is it possible to pass-through or pipe the request directly to the client, or how can I also send the header as well as the body ? 是否可以将请求直接传递或通过管道传递给客户端,或者我如何还可以发送标头和正文?

You can pipe the request directly to the response: 您可以将请求直接传递给响应:

var request = require('request');
var url = "http://localhost:8080/?somevar=someval";
request(url).pipe(res);

Depending on your what you are actually doing with the original request on your code you may actually do a one line proxy. 根据您对代码上原始请求的实际处理方式,您实际上可以执行一行代理。 See here : 这里

req.pipe(request('http://example.com/doodle.png')).pipe(resp)

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

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