app.get - is there any difference between res.send vs return res.send

后端 未结 3 574
误落风尘
误落风尘 2020-12-25 09:42

I am new to node and express. I have seen app.get and app.post examples using both \"res.send\" and \"return res.send\". Are these the same?

var express = r         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-25 10:28

    The return keyword returns from your function, thus ending its execution. This means that any lines of code after it will not be executed.

    In some circumstances, you may want to use res.send and then do other stuff.

    app.get('/', function(req, res) {
      res.send('i am a beautiful butterfly');
      console.log("this gets executed");
    });
    
    app.get('/', function(req, res) {
      return res.send('i am a beautiful butterfly');
      console.log("this does NOT get executed");
    });
    

提交回复
热议问题