I just started learning Node.js, and I'm wondering when developer should/need to use http.createServer()
My process of thinking is: "When I want to catch data from server I use: http.request
and http.get
methodes.
When I want to POST on server I will use http.request
with POST method options parameter.
I just can't figure out, when I should use http.createServer()
method?
If I work on remote server (server is already exists).
I red documentation: This class is used to create a TCP or IPC server.
w3School says: The http.createServer() method turns your computer into an HTTP server.
If you can, give me comprehensive explanation with real world example.
Thanks
When you use something like http.request()
, you're connecting to another HTTP server and requesting data from it. If you wanted to be that server yourself, you would need to use http.createServer()
to do that.
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. It takes the resulting data and sends it as an HTTP response.
The Node.js documentation you read seems to be for net.createServer()
. While http.createServer()
does create an underlying TCP server, it also accepts a callback for listening for HTTP requests.
See also: https://nodejs.org/api/http.html#http_http_createserver_options_requestlistener
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.