response.setHeader(name, value)(在 v0.4.0 中添加)方法是“http”模块的内置应用程序编程接口,它为隐式标头设置单个标头值。如果此标头已存在于 to-be-sent 标头中,则其值将被替换。在此处使用字符串数组发送多个具有相同名称的标头。非字符串值将不加修改地存储。因此, response.getHeader() 可能会返回非字符串值。但是,非字符串值将被转换为字符串以进行网络传输。
如果已使用response.setHeader()设置标头,则它们将与传递给response.writeHead()的任何标头合并,并且标头传递给response.writeHead()优先。
为了获得响应和正确的结果,我们需要导入‘http’模块。
用法:
const http = require('http');
用法:
response.setHeader(name, value)
参数:该属性接受上述和以下描述的单个参数:
- name <String>:它接受标题的名称,不区分大小写。
- value <any>:它可以接受任何值,如对象、字符串、整数、数组等。
返回值:它不返回任何值,而是按如下所述设置标头。
下面的示例说明了在 Node.js 中使用 response.setHeader() 属性。
范例1: 文件名:index.js
// Node.js program to demonstrate the
// response.setHeaders() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(request, response) {
// Setting up Headers
response.setHeader('Content-Type', 'text/html');
response.setHeader('Set-Cookie', ['type=ninja',
'language=javascript']);
// Checking and printing the headers
console.log("When Header is set a string:",
response.getHeader('Content-Type'));
console.log("When Header is set an Array:",
response.getHeader('Set-Cookie'));
// Getting the set Headers
const headers = response.getHeaders();
// Printing those headers
console.log(headers);
// Prints Output on the browser in response
response.writeHead(200,
{ 'Content-Type':'text/plain' });
response.end('ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log("Server is running at port 3000...");
});
现在在浏览器中运行http://localhost:3000 /。
输出:
In Console
>> Server is running at port 3000…
>> When Header is set a string:text/html
>> When Header is set an Array:[‘type=ninja’, ‘language=javascript’]
>> [Object:null prototype] { ‘content-type’:‘text/html’, ‘set-cookie’:[‘type=ninja’, ‘language=javascript’]}
范例2: 文件名:index.js
// Node.js program to demonstrate the
// response.setHeaders() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
function(req, response) {
// Setting up Headers
response.setHeader('Alfa', 'Beta');
response.setHeader('Alfa1', '');
response.setHeader('Alfa2', 5);
response.setHeader('Cookie-Setup',
['Alfa=Beta', 'Beta=Romeo']);
// response.setHeader('', 'Beta'); // Throws Error
// response.setHeader(); // Throws Error
// Checking and printing the headers
console.log("When Header is set an Array:",
response.getHeader('Cookie-Setup'));
console.log("When Header is set an 'Beta':",
response.getHeader('Alfa'));
console.log("When Header is set '':",
response.getHeader('Alfa1'));
console.log("When Header is set number 5:",
response.getHeader('alfa2'));
console.log("When Header is not set:",
response.getHeader('Content-Type'));
// Getting the set Headers
const headers = response.getHeaders();
// Printing those headers
console.log(headers);
var Output = "Hello Geeksforgeeks..., "
+ "Available headers are:"
+ JSON.stringify(headers);
// Prints Output on the browser in response
response.write(Output);
response.end('ok');
});
// Listening to http Server
httpServer.listen(PORT, () => {
console.log("Server is running at port 3000...");
});
使用以下命令运行index.js文件:
node index.js
输出:
Output: In Console
Server is running at port 3000…
When Header is set an Array:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ]
When Header is set an ‘Beta’:Beta
When Header is set ”:
When Header is set number 5:5
When Header is not set:undefined
[Object:null prototype] {
alfa:‘Beta’,
alfa1:”,
alfa2:5,
‘cookie-setup’:[ ‘Alfa=Beta’, ‘Beta=Romeo’ ]}
现在在浏览器中运行http://localhost:3000 /。
Output: In Browser
Hello Geeksforgeeks…, Available headers are:{“alfa”:”Beta”, “alfa1″:””, “alfa2”:5, “cookie-setup”:[“Alfa=Beta”, “Beta=Romeo”]}ok
如果调用了 response.writeHead() 方法而该方法没有被调用,它将直接将提供的 header 值写入网络通道而不进行内部缓存,并且 header 上的 response.getHeader() 不会产生预期的结果。如果需要在未来可能检索和修改标头的渐进式填充,请使用 response.setHeader() 而不是 response.writeHead()。
参考: https://nodejs.org/api/http.html#http_response_setheader_name_value
相关用法
- Node.js process.nextTick()用法及代码示例
- Node.js GM solarize()用法及代码示例
- Node.js MySQL Max()用法及代码示例
- Lodash _.method()用法及代码示例
- Node.js Http2ServerRequest.method用法及代码示例
- Node.js http.IncomingMessage.method用法及代码示例
- Collect.js toArray()用法及代码示例
- Javascript RegExp toString()用法及代码示例
- Tensorflow.js tf.Sequential.evaluate()用法及代码示例
- Node.js URLSearchParams.has()用法及代码示例
- JavaScript Math cosh()用法及代码示例
- Node.js hmac.update()用法及代码示例
- jQWidgets jqxFormattedInput val()用法及代码示例
- HTML DOM isEqualNode()用法及代码示例
- JavaScript Date toLocaleTimeString()用法及代码示例
- Tensorflow.js tf.Tensor.buffer()用法及代码示例
注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js response.setHeader() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。