Express/node.js 204 HTTP代码响应问题 [英] Express/node.js 204 HTTP code response issue

查看:345
本文介绍了Express/node.js 204 HTTP代码响应问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

.put(function(req, res) {
  User.findById(req.params.user_id, function(err, user) {
    if(err) return res.send(err);

    user.dateEdited = new Date();
    user.save(function(err) {
      if(err) return res.send(err);

      return res.status(204).json(customHTTPcodeReponses.updated(user))
    });
  });
});      

我的中间件的一部分称为customHTTPcodeReponses

Part of my middleware called customHTTPcodeReponses

updated: function(data) {
  return {
    code: 204,
    status: 'Success',
    message: 'Resource updated (or soft deleted)',
    data: data
  };
}

我发现,204不应该返回任何数据,所以我没有得到任何返回. 但是我想拥有这些数据,以了解真正发生了什么变化.那我该如何处理响应代码?

I as figured out, 204 is not supposed to return any data, so I am not getting any back. But I would like to have this data to see what was really changed. How could I hande response code then?

请记住,如果我使用

res.status(200).json(customHTTPcodeReponses.updated(user))

显示

数据.

如果您需要其他说明,请询问.

If you need some extra explanation, please ask.

推荐答案

是的,您是正确的.此http状态不允许的消息,因为它表示无内容".如果您发送内容,请使用其他状态.有关详细信息,请参见文档: http://www.w3.org/Protocols/rfc2616 /rfc2616-sec10.html . 有一部分:"204响应必须不包含消息正文,因此总是由标头字段之后的第一个空行终止."

yes, you are correct. This http status not allowed messages because it means "No Content". If you send content, use other statuses. For details look at document: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. There is part: "The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields."

如果您要发送的不是带有内容信息的元数据,而是元数据,请在本文档中包含以下内容:响应中可能包含实体标题形式的新的或更新的元信息,如果存在,则应将其与所请求的变体相关联".我认为这是您问题的正式答案.

If you want to send not content but metadata with addictional info, in this document there is part : "The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.". I think it is formal answer for your question.

如果您不那么正式,请使用状态'200'和元数据作为内容.

If you not so formal, use status '200' with metadata as content.

在标头中发送数据:

res.header(field, [value])

这篇关于Express/node.js 204 HTTP代码响应问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆