Node.js response.setHeader() 方法
response.setHeader(name, value) (在 v0.4.0 中添加)方法是“ http ”模块的内置应用程序编程接口,它为隐式标头设置单个标头值。如果这个头已经存在于待发送的头中,它的值将被替换。在此处使用字符串数组发送多个具有相同名称的标头。非字符串值将不加修改地存储。因此, response.getHeader()可能会返回非字符串值。但是,非字符串值将被转换为字符串以进行网络传输。
当使用response.setHeader()设置了 headers 时,它们将与传递给 response.writeHead() 的任何 headers 合并,并且传递给response.writeHead ()的 headers 具有优先级。
为了得到响应和正确的结果,我们需要导入“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