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

雨燕双飞 提交于 2019-12-01 01:58:50

问题


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  
});

回答1:


No, this is not possible without rewriting or refactoring your code. The reason is that res.send actually calls res.end after 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 /mycall1 and /mycall2 call separate functions internally, and /myAggregate calls both the functions.

In these functions, you would have to use res.write to prevent ending the response. The handlers for /mycall1, /mycall2, and /myAggregate would each have to call res.end separately to actually end the response.




回答2:


Like many things in javascript your original goal can be accomplished with sneakiness. We can overwrite the res.send function so that it doesn't call res.end; this will allow res.send to be called multiple times without issue. Note that this is an ugly, sneaky approach - not recommended, but potentially useful:

app.get('myAggregate', (req, res) => {
  // Overwrite `res.send` so it tolerates multiple calls:
  let restoreSend = res.send;
  res.send = () => { /* do nothing */ };

  // Call mycall1
  req.method = 'GET';
  req.url = '/mycall1';
  app.handle(req, res, () => {});

  // Call mycall2
  req.method = 'GET';
  req.url = '/mycall2';
  app.handle(req, res, () => {});

  // Restore `res.send` to its normal functionality
  res.send = restoreSend;

  // Finally, call `res.send` in conclusion of calling both mycall1 and mycall2
  res.send('Good AND Good2!');

});


来源:https://stackoverflow.com/questions/9474796/in-express-or-connect-with-node-js-is-there-a-way-to-call-another-route-interna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!