Moduł HTTP Node.js
Przykład
Utwórz serwer, który nasłuchuje na porcie 8080 twojego komputera.
Po uzyskaniu dostępu do portu 8080 napisz „Hello World!” z powrotem w odpowiedzi:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definicja i użycie
Moduł HTTP zapewnia sposób na to, aby Node.js przesyłał dane przez HTTP (Hyper Text Transfer Protocol).
Składnia
Składnia dołączania modułu HTTP do Twojej aplikacji:
var http = require('http');
Właściwości i metody HTTP
Method | Description |
---|---|
createClient() | Deprecated. Creates a HTTP client |
createServer() | Creates an HTTP server |
get() | Sets the method to GET, and returns an object containing the user's request |
globalAgent | Returns the HTTP Agent |
request() | Returns an object containing the user's request |