简体   繁体   English

访问自定义请求标头节点表达

[英]Access custom request headers node express

I am building a web api with Express and have not found information on accessing incoming custom request headers. 我正在使用Express构建Web api,但未找到有关访问传入的自定义请求标头的信息。

I will be expecting, for instance, that an incoming post request have a provider_identifier header. 例如,我期望传入的帖子请求具有provider_identifier标头。 When I receive the request, I need to access that header information to validate their subscription. 当我收到请求时,我需要访问该标头信息以验证其订阅。

Can someone point me in the right direction/provide advice on this? 有人能指出我正确的方向/就此提出建议吗?

router.post('myendpoint/', function(req, res){
    var providerId = req.????;
});

Answering my own question here... was kindof a DUH moment for me. 在这里回答我自己的问题......对我来说是个DUH时刻。

Using above example, simply reference the headers collection like so: 使用上面的示例,只需引用标题集合,如下所示:

var providerId = req.headers.provider_identifier;

One note: Use an underscore rather than a dash. 一个注意事项:使用下划线而不是短划线。 "provider-identifier" doesn't work, but "provider_identifier" does. “provider-identifier”不起作用,但“provider_identifier”可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM