Modulo HTTP Node.js
Esempio
Crea un server in ascolto sulla porta 8080 del tuo computer.
Quando si accede alla porta 8080, scrivi "Hello World!" indietro come risposta:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definizione e utilizzo
Il modulo HTTP fornisce un modo per fare in modo che Node.js trasferisca i dati su HTTP (Hyper Text Transfer Protocol).
Sintassi
La sintassi per includere il modulo HTTP nell'applicazione:
var http = require('http');
Proprietà e metodi 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 |