[英]When we use http.createServer() in Node.js
我刚开始学习 Node.js,我想知道开发人员何时应该/需要使用http.createServer()
我的思考过程是:“当我想从我使用的服务器捕获数据时: http.request
和http.get
方法。
当我想在服务器上发布时,我将使用http.request
和 POST 方法选项参数。
我只是想不通,什么时候应该使用http.createServer()
方法?
如果我在远程服务器上工作(服务器已经存在)。
我红色文档: This class is used to create a TCP or IPC server.
w3School 说: The http.createServer() method turns your computer into an HTTP server.
如果可以的话,用现实世界的例子给我全面的解释。
谢谢
当您使用http.request()
类的东西时,您将连接到另一个 HTTP 服务器并从中请求数据。 如果您想自己成为该服务器,则需要使用http.createServer()
来做到这一点。
For example, a common use case of Node.js is to create an API server that receives HTTP requests from web pages and fetches or manipulates data in a database. 它获取结果数据并将其作为 HTTP 响应发送。
您阅读的 Node.js 文档似乎适用于net.createServer()
。 虽然http.createServer()
确实创建了一个底层 TCP 服务器,但它也接受一个回调来监听 HTTP 请求。
另见: https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.