So, I have the setup like this (in Express):
app.get('/mycall1', function(req,res) { res.send('Good'); });
app.get('/mycall2', function(req,res) { res.send('Good2'); });
What if I want make an aggregate function to call /mycall1 and /mycall2 without rewriting code and reusing code for /mycall1 and /mycall2?
For example:
app.get('/myAggregate', function (req, res) {
// call /mycall1
// call /mycall2
});
No, this is not possible without rewriting or refactoring your code. The reason is that
res.sendactually callsres.endafter it is done writing. That ends the response and nothing more can be written.As you hinted to, you can achieve the desired effect by refactoring the code so that both
/mycall1and/mycall2call separate functions internally, and/myAggregatecalls both the functions.In these functions, you would have to use
res.writeto prevent ending the response. The handlers for/mycall1,/mycall2, and/myAggregatewould each have to callres.endseparately to actually end the response.