Home > Web Front-end > JS Tutorial > Briefly compare setHeader and writeHead in Node and talk about the differences.

Briefly compare setHeader and writeHead in Node and talk about the differences.

青灯夜游
Release: 2022-03-23 20:04:10
forward
2629 people have browsed it

NodejsWhat is the difference between setHeader and writeHead? The following article will compare setHeader and writeHead and talk about their differences. I hope it will be helpful to everyone!

Briefly compare setHeader and writeHead in Node and talk about the differences.

Today when I was studying Node, I discovered setHeader# in Node/http ## is very similar to writeHead. Both can set the response header. Let’s talk about it in detail!

setHeader

Parameter

response.setHeader(name, value)复制代码
Copy after login
    name Attribute
  • value Attribute value
  • Return
  • http.ServerResponse Return the response object

Function

Set a single attribute for the response header.

Note

    You can only set attributes one by one
    Repeatedly setting an attribute will replace the previous setting
    Setting an attribute field name or value that contains invalid characters will cause a
  • TypeError

Example

reponse.setHeader('Content-Type', 'text/html')
Copy after login
reponse.setHeader('Set-Cookie', ['type=ninja', 'language=javascript'])
Copy after login

Repeatedly set an attribute

// 返回 content-type = text/html1
reponse.setHeader('Content-Type', 'text/html')
reponse.setHeader('Content-Type', 'text/html1')
Copy after login

Briefly compare setHeader and writeHead in Node and talk about the differences.

##writeHead

Parameter

response.writeHead(statusCode, [statusMessage], [headers])
Copy after login

    statusCode http status code
  • statusMessage status message (optional)
  • headers | Property object or array (optional)
  • Return
  • http.ServerResponse

    Return response object

Briefly compare setHeader and writeHead in Node and talk about the differences.

Function

has the same effect as

setHeader

Note

    Multiple attributes can be set, setHeader can only set one
  • can only be called once
  • must be in
  • response.end ()

    Called before

  • Setting an attribute field name or value that contains invalid characters will cause
  • TypeError

    # to be thrown
  • ##Example

Because writeHead returns a ServerResponse object, we can make chain calls

const body = 'hello world';
response
  .writeHead(200, {
    'Content-Length': Buffer.byteLength(body),
    'Content-Type': 'text/plain'
  })
  .end(body);
Copy after login

The Content-Length here is in bytes, and Not characters. Buffer.byteLength() is used to determine the length of the text.

Nodejs will not check whether Content-Length is consistent with the length of the transmitted body

Use setHeader and writeHead at the same time

// 返回 content-type = text/plain
const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('X-Foo', 'bar');
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('ok');
});
Copy after login
writeHead has a higher priority than

setHeader, and writeHead can only be called once, so when calling, first consider which headers do not change often, and then call writeHeadIf setHeader

has been called to set the header, then it will be passed to

writeHead mergeIf this method is called, and response has not been called yet .setHeader()

), the provided header value will be written directly to the network channel and will not be cached internally.

response.getHeader()) on the header does not produce the expected results. If the header needs to be populated incrementally with potential future retrieval and modification, use response.setHeader() instead. Summary

##setHeader can only set headers one by one, writeHead can set many at once

  • setHeader can be called repeatedly, writeHead can only be called once

  • setHeader and writeHead appear at the same time, setHeader will be merged into writeHead, and writeHead has a high priority

  • writeHead can set the status code and status information, setHeader cannot be set, only the header can be set

  • ##For more node-related knowledge, please visit:

    nodejs tutorial

    !

The above is the detailed content of Briefly compare setHeader and writeHead in Node and talk about the differences.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template