NodeJs 中的 response.writeHead 和 response.end

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

response.writeHead and response.end in NodeJs

node.js

提问by Kevin

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);
  1. Can anyone explain me why do we call the writeHeadand endmethod in createServer method.
  2. What is the main purpose of options object passed in createServer method.
  1. 谁能解释一下为什么我们 在 createServer 方法中调用writeHeadandend方法。
  2. 在 createServer 方法中传递的选项对象的主要目的是什么。

回答by Frank van Puffelen

Those calls to writeHeadand endare not being done in the createServermethod, but rather in a callback.

那些对writeHead和 的调用end不是在createServer方法中完成的,而是在回调中完成的。

It's a bit easier to see if you split out the callback into a separate function:

如果将回调拆分为单独的函数,则更容易查看:

function handleRequest(req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}

https.createServer(options, handleRequest).listen(8000);

So here we define a handleRequestfunction and then pass that into the createServercall. Now whenever the node.js server we created receives an incoming request, it will invoke our handleRequestmethod.

所以在这里我们定义一个handleRequest函数,然后将它传递给createServer调用。现在,每当我们创建的 node.js 服务器收到传入请求时,它都会调用我们的handleRequest方法。

This pattern is very common in JavaScript and is core to node.js' asynchronous event handling.

这种模式在 JavaScript 中非常常见,并且是 node.js 异步事件处理的核心。

回答by Gui Imamura

In your code, the writeHead()is called to write the header of the response, that the application will serve to the client. The end()method both sends the content of the response to the client and signals to the server that the response (header and content) has been sent completely. If you are still going to send anything else, you should call write()method of resresponse object instead.

在您的代码中,writeHead()调用 来编写响应的标头,应用程序将提供给客户端。该end()方法既向客户端发送响应内容,又向服务器发送响应(标头和内容)已完全发送的信号。如果您仍要发送其他任何内容,则应改为调用响应对象的write()方法res

The optionsJSON object is a modifier that you may use, to override the default behaviour of the createServer()method. In your code's case:
+ key: Private key to use for SSL (default is null)
+ cert: Public x509 certificate to use (default is null)

optionsJSON对象是一个修改器,你可以用,要覆盖的默认行为createServer()方法。在您的代码的情况下:
+ 密钥:用于 SSL 的私钥(默认为空)
+ 证书:要使用的公共 x509 证书(默认为空)

You can find more in this sectionof the Node.js API doc about the response.writeHead()method.
You can find more in this sectionof the Node.js API doc about the https.createServer()method.

您可以在 Node.js API 文档的这一部分中找到有关该response.writeHead()方法的更多信息。
您可以在 Node.js API 文档的这一部分中找到有关该https.createServer()方法的更多信息。

回答by Mr.7

response.writeHead(200)sends a response header to the request. The status code is a 3-digit HTTP status code, like 404.

response.writeHead(200)向请求发送响应头。状态码是一个 3 位的 HTTP 状态码,如 404。

This method must only be called once on a message and it must be called before response.end() is called.

此方法只能在消息上调用一次,并且必须在调用 response.end() 之前调用。

If you call response.write() or response.end() before calling this, the implicit/mutable headers will be calculated and call this function for you.

如果在调用之前调用 response.write() 或 response.end() ,将计算隐式/可变标头并为您调用此函数。

回答by kunal pal

As far as i know if you don't put the response.end() at the end then your web page will go on loading thus the response.end() is used to tell the server that the data has been loaded

据我所知,如果您不将 response.end() 放在最后,那么您的网页将继续加载,因此 response.end() 用于告诉服务器数据已加载