How to res.send to a new URL in Node.js/Express?
Asked Answered
L

3

5

Given a GET request to URL X, how should you define res.send such that it provides a response to a completely separate URL Y?

i.e.

app.get('/', function (req, res) {
    res.send to a new URL external of the app ('Success')
});

Thanks and apologies in advance for ignorance on the topic!

Lizzettelizzie answered 8/11, 2016 at 22:29 Comment(3)
Do you mean having one route redirect to another on completion?Misuse
Hi Matthew, did any of the answers below help you? If you feel that one of them answered your question, I'm sure they'd appreciate if you mark their answer as correct. If you still need help, just comment here or on one of their answers!Scales
Sorry to clarify - I don't want to redirect the request itself to another URL. When the GET request comes in to the specified route, I want to trigger a POST to another URL entirely, providing in the body, some of the data from the GET request, but not the request itself. It sounds from the below answers that the POST to the external URL takes place not in the (req, res) but as a separate, defined request. Reason being that res can only pass back to the request source - If I'm understanding correctly @rocketspacer!Lizzettelizzie
T
4

You can only send response to the request source, which is the client. There is no such thing as sending response to another "external-url" (or another server).
However, you CAN make a request to another server, wait for it's response, and respond to our client.

app.get('/', (req, res) => {
    
    var requestOptions = {
        hostname: 'serverB.com', // url or ip address
        port: 8080, // default to 80 if not provided
        path: '/take-request',
        method: 'POST' // HTTP Method
    };

    var externalRequest = http.request(requestOptions, (externalResponse) => {
        
        // ServerB done responding
        externalResponse.on('end', () => {
            
            // Response to client
            res.end('data was send to serverB');
        });
    });
    
    // Free to send anthing to serverB
    externalRequest.write(req.data);
    externalRequest.end();
});
Taler answered 8/11, 2016 at 22:55 Comment(6)
Whilst this answer is correct I think it's worth mentioning that this is not what you might do under normal circumstances as it may be infringing copyright and who knows what other legal hard spots. Not to mention being logically incorrect (displaying the contents of foo_url in response to bar_url). What the op is really looking for is a redirect, IMO.Scales
No legal issues if you own both servers, right ? Like in microservices system.Taler
I think OP means to send data (response) to another "external-url" not redirecting to oneTaler
Perhaps, only the OP knows :p In any case, I think it's usually more appropriate to redirect so that the url bar and history is correct etc. I'm sure there are some use cases, as you say with microservices.Scales
OP here haha, @rocketspacer you're correct - sorry for confusionLizzettelizzie
I don't want to redirect the request itself to another URL. When the GET request comes in to the specified route, I want to trigger a POST to another URL entirely, providing in the body, some of the data from the GET requestLizzettelizzie
B
11

You want to redirect the request by setting the status code and providing a location header. 302 indicates a temporary redirect, 301 is a permanent redirect.

app.get('/', function (req, res) {
  res.statusCode = 302;
  res.setHeader("Location", "http://www.url.com/page");
  res.end();
});
Bloodmobile answered 8/11, 2016 at 22:39 Comment(0)
T
4

You can only send response to the request source, which is the client. There is no such thing as sending response to another "external-url" (or another server).
However, you CAN make a request to another server, wait for it's response, and respond to our client.

app.get('/', (req, res) => {
    
    var requestOptions = {
        hostname: 'serverB.com', // url or ip address
        port: 8080, // default to 80 if not provided
        path: '/take-request',
        method: 'POST' // HTTP Method
    };

    var externalRequest = http.request(requestOptions, (externalResponse) => {
        
        // ServerB done responding
        externalResponse.on('end', () => {
            
            // Response to client
            res.end('data was send to serverB');
        });
    });
    
    // Free to send anthing to serverB
    externalRequest.write(req.data);
    externalRequest.end();
});
Taler answered 8/11, 2016 at 22:55 Comment(6)
Whilst this answer is correct I think it's worth mentioning that this is not what you might do under normal circumstances as it may be infringing copyright and who knows what other legal hard spots. Not to mention being logically incorrect (displaying the contents of foo_url in response to bar_url). What the op is really looking for is a redirect, IMO.Scales
No legal issues if you own both servers, right ? Like in microservices system.Taler
I think OP means to send data (response) to another "external-url" not redirecting to oneTaler
Perhaps, only the OP knows :p In any case, I think it's usually more appropriate to redirect so that the url bar and history is correct etc. I'm sure there are some use cases, as you say with microservices.Scales
OP here haha, @rocketspacer you're correct - sorry for confusionLizzettelizzie
I don't want to redirect the request itself to another URL. When the GET request comes in to the specified route, I want to trigger a POST to another URL entirely, providing in the body, some of the data from the GET requestLizzettelizzie
G
0

You can't change the url you send back but you can change the url that's visible in the browser after it's loaded. See How do I modify the URL without reloading the page? for various options.

Graben answered 7/8, 2024 at 4:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.