如何使用 Node.js / Express 发回响应标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7676264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:39:47  来源:igfitidea点击:

How can I send back response headers with Node.js / Express?

node.jsexpressresponse-headers

提问by Shamoon

I'm using res.sendand no matter what, it returns status of 200. I want to set that status to different numbers for different responses (Error, etc)

我正在使用res.send,无论如何,它返回 200 的状态。我想将该状态设置为不同的数字以用于不同的响应(错误等)

This is using express

这是使用 express

回答by Z. Khullah

For adding response headers beforesend, you can use the setHeadermethod:

发送添加响应头,可以使用setHeader方法:

response.setHeader('Content-Type', 'application/json')

The status onlyby the statusmethod:

状态只能status方法:

response.status(status_code)

Bothat the same time with the writeHeadmethod:

两者同时使用writeHead方法:

response.writeHead(200, {'Content-Type': 'application/json'});

回答by Nican

I'll assume that you're using a library like "Express", since nodejs doesn't provide ares.sendmethod.

我假设您使用的是像“Express”这样的库,因为 nodejs 不提供res.send方法。

As in the Express guide, you can pass in a second optional argument to send the response status, such as:

Express guide 中,您可以传入第二个可选参数来发送响应状态,例如:

// Express 3.x
res.send( "Not found", 404 );

// Express 4.x
res.status(404).send("Not found");

回答by mbokil

Since the question also mentions Express you could also do it this way using middleware.

由于该问题还提到 Express,您也可以使用中间件以这种方式进行。

app.use(function(req, res, next) {
  res.setHeader('Content-Type', 'text/event-stream');
  next();
});

回答by Naveen Kumar V

You should use setHeadermethod and statusmethod for your purpose.

您应该为您的目的使用setHeader方法和status方法。

SOLUTION:

解决方案:

app.post('/login', function(req, res) {

  // ...Check login credentials with DB here...

  if(!err) {
    var data = {
      success: true,
      message: "Login success"
    };

    // Adds header
    res.setHeader('custom_header_name', 'abcde');

    // responds with status code 200 and data
    res.status(200).json(data);
  }
});

回答by cs04iz1

In the documentation of express(4.x) the res.sendStatus is used to send status code definitions. As it is mentioned hereeach has a specific description.

express(4.x)的文档中, res.sendStatus 用于发送状态代码定义。正如这里提到的每个都有特定的描述。

res.sendStatus(200); // equivalent to res.status(200).send('OK')
res.sendStatus(403); // equivalent to res.status(403).send('Forbidden')
res.sendStatus(404); // equivalent to res.status(404).send('Not Found')
res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')

回答by Yura

set statusCodevar before send() call

在 send() 调用之前设置statusCodevar

res.statusCode = 404;
res.send();

回答by fuelusumar

i use this with express

我用快递

res.status(status_code).send(response_body);

and this without express (normal http server)

而这没有快递(普通的http服务器)

res.writeHead(404, {
    "Content-Type": "text/plain"
});
res.write("404 Not Found\n");
res.end();