简体   繁体   中英

Nodejs - serve a request from tomcat running on same server

I have a nodejs server running on port 3000 works fine.

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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