In Express or connect to Node.js, is there a way to call another route inland?

So, I have this setup (in Express):

app.get('/mycall1', function(req,res) { res.send('Good'); }); app.get('/mycall2', function(req,res) { res.send('Good2'); }); 

What if I want to make an aggregate function to call /mycall1 and /mycall2 without rewriting the code and reusing the code for /mycall1 and /mycall2 ?

For instance:

 app.get('/myAggregate', function (req, res) { // call /mycall1 // call /mycall2 }); 
+6
source share
1 answer

No, this is not possible without rewriting or refactoring the code. The reason is that res.send actually calls res.end after it is written . This completes the answer, and nothing else can be written.

As you hinted, you can achieve the desired effect by editing the code so that both /mycall1 and /mycall2 call separate functions inside, and /myAggregate call both functions.

In these functions, you will need to use res.write to prevent the response from completing. The handlers for /mycall1 , /mycall2 and /myAggregate had to call res.end each time to actually complete the response.

+7
source

Source: https://habr.com/ru/post/909504/


All Articles