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

前端 未结 2 959
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 13:49

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.         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 14:43

    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.

提交回复
热议问题