forked from MartinChavez/Node.js-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09 - Http Module.js
More file actions
30 lines (26 loc) · 838 Bytes
/
09 - Http Module.js
File metadata and controls
30 lines (26 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* Http request Module */
// The following example demonstrates how to create an http request with Node.js
var http = require('http');
var makeRequest = function (message) {
// Define the options for the http request
var options = {
host: 'localhost', port: 8080, path: '/', method: 'POST'
}
// Initialize the request
var request = http.request(options, function (response) {
// Specifies the callback function when data gets received
response.on('data', function (data) {
// In this case, logs the response body
console.log(data.toString());
});
});
// Begins the request
request.write(message);
// Finish the request
request.end();
}
// Declare the global function
module.exports = makeRequest;
/*
To run, go to "8 - Module Loader"
*/