本文介绍了app.get-res.send与return res.send之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是node和express的新手.我看过同时使用"res.send"和"return res.send"的app.get和app.post示例.这些都一样吗?
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 = require('express'); var app = express(); app.get('/', function(req, res) { res.type('text/plain'); res.send('i am a beautiful butterfly'); });或
var express = require('express'); var app = express(); app.get('/', function(req, res) { res.type('text/plain'); return res.send('i am a beautiful butterfly'); });推荐答案
return关键字从函数中返回,从而结束其执行.这意味着之后的任何代码行都不会被执行.
The return keyword returns from your function, thus ending its execution. This means that any lines of code after it will not be executed.
在某些情况下,您可能想使用res.send然后再做其他事情.
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"); });更多推荐
app.get
发布评论