Javascript 使用 node.js 读取传入的 HTTP 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4546338/
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-08-23 12:53:59  来源:igfitidea点击:

Reading incoming HTTP headers with node.js

javascriptjsonvariableshttp-headersnode.js

提问by M0rph3v5

Now as example, I'm getting an response which has partially the key/values as an javascript object:

现在作为示例,我收到一个响应,其中部分键/值作为 javascript 对象:

status: '200 OK',
'content-encoding': 'gzip'

I can easily read out and log the status message by: headers.status but when I try to log the content-encoding (which I need in this particular situation) it errors on:

我可以通过以下方式轻松读出并记录状态消息: headers.status 但是当我尝试记录内容编码(在这种特殊情况下我需要)时,它会出错:

headers.'content-encoding' <- obviously the quotes it doesn't like
headers.content-encoding <- obviously the '-' it doesn't like

How am I suppose to get/read/log it's content-encoding value?

我该如何获取/读取/记录它的内容编码值?

Greets,

问候,

m0rph3v5

m0rph3v5

回答by Sanjay T. Sharma

Javascript also supports square bracket notation for referring to properties so if headersis an appropriate object, you can use headers['content-encoding'].

Javascript 还支持用于引用属性的方括号表示法,因此如果headers是合适的对象,您可以使用headers['content-encoding'].

回答by T.J. Crowder

JavaScript properties have names as you know. When the name is a legal identifier and you know the literal name you want when you're writing the code, you can use it with dotted notation.

正如您所知,JavaScript 属性具有名称。当名称是合法标识符并且您在编写代码时知道所需的字面名称时,您可以使用带点符号的名称。

var foo = headers.foo;

When the name isn't a legal identifier, or if you want to determine the name you're looking up at runtime, you can use a string:

当名称不是合法标识符时,或者如果您想确定在运行时查找的名称,您可以使用字符串:

var encoding = headers['content-encoding'];

or

或者

var name = 'content-encoding';
var encoding = headers[name];

or even

甚至

var x = 'encoding';
var encoding = headers['content-' + x];

As you can see, it doesn't have to be a literalstring. This is very handy for general-purpose functions that have to accept a property name as a function argument or similar.

如您所见,它不必是文字字符串。这对于必须接受属性名称作为函数参数或类似参数的通用函数非常方便。

Note that property names are case sensitive.

请注意,属性名称区分大小写。

回答by Alfred

I think you should install the very good expressframework. I really simplifies node.js webdevelopment.

我认为您应该安装非常好的express框架。我真的简化了 node.js 网络开发。

You could install it using npm

你可以使用 npm 安装它

npm install express

This snippet shows you how to set headers and read headers

此代码段向您展示了如何设置标题和读取标题

var express = require('express');

var app = express.createServer();

app.get('/', function(req, res){
    console.log(req.header('a'));
    res.header('time', 12345);

    res.send('Hello World');
});

app.listen(3000);

Curl from command line

从命令行卷曲

$curl http://localhost:3000/ -H "a:3434" -v
* About to connect() to localhost port 3000 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.2 (i686-pc-linux-gnu) libcurl/7.21.2 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: localhost:3000
> Accept: */*
> a:3434
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< time: 12345
< Content-Type: text/html; charset=utf-8
< Content-Length: 11
< Date: Tue, 28 Dec 2010 13:58:41 GMT
< X-Response-Time: 1ms
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
* Closing connection #0
Hello World

The log outputting the header send via curl to node server:

输出头的日志通过 curl 发送到节点服务器:

$ node mo.js 
3434