Modul HTTP Node.js
Contoh
Buat server yang mendengarkan pada port 8080 komputer Anda.
Ketika port 8080 sudah diakses, tulis "Hello World!" kembali sebagai tanggapan:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definisi dan Penggunaan
Modul HTTP menyediakan cara membuat Node.js mentransfer data melalui HTTP (Hyper Text Transfer Protocol).
Sintaksis
Sintaks untuk menyertakan modul HTTP dalam aplikasi Anda:
var http = require('http');
Properti dan Metode 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 |