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

or

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.type('text/plain');
  return res.send('i am a beautiful butterfly');
});

推荐答案

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

Node.js相关问答推荐

运行JEST测试时找不到模块&q;错误

如何在MongoDB中更新嵌套数组

FireStore事务似乎已允许并发编辑?

将图像添加到多个产品的条带 checkout 会话中

如何获取文件的中间值?

如何通过node下载zip并直接解压zip?

等待不在 Express.js 中处理 res.app.render

node-gyp: "..\src\binding.cc: 没有这样的文件或目录"

将代码转换为 ES6 Discord.js 的问题

TypeError:在使用 Jest、Supertest、Express、Typescript 进行测试时无法读取未定义的属性(读取listen)

使用 Forms API 进行批量更新时生成 itemId

带有事件网格的 Azure 函数在没有 ngrok 的情况下在本地运行

如何在 Node.js 中逐字节读取二进制文件

适用于 Windows 7 的 NodeJS

Node.js + Express 上的多个视图路径

如何为 node.js 服务器分配域名?

npm publish 导致'错误:EPERM:不允许操作,取消链接...',errno -4048

如何在 NodeJs 中下载和解压缩内存中的 zip 文件?

如何在 MongoDB 中查询引用的对象?

node.js 中存储的模块变量在什么范围内?