HTTP

The purpose of this guide is to give you understanding of how the core http API with Node works.

Express and hapi frameworks use underneath the builtin http module.

With this module you can create an http server and much more.

This is the way you create the basic http server code:

var http = require('http');

var server = http.createServer(function(request, response) {
  // http server implementation
});```

The callback function that that is passed to the httpserver is called ebery time there is a request from the client.   

Http headers can be manipulates and retrieved from headers object:

var headers = request.headers;
var userAgent = headers['user-agent'];```

The http properties can be also retrievd as follows:

var url = request.url;
var httpmethod = request.method;```

1. In order to perform a **simple get** with the http module :

var http = require('http');
var options = {
host: 'www.stackoverflow.com',
path: '/index.html'
};

var req = http.get(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));

// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
console.log('BODY: ' + body);
// ...and/or process the entire body here.
})
});
//because it implements event emitter itf on error is defined
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});```

This get method calls the req.end() automatically

Another way is to use the general http.request(options, callback) function which allows you to specify the request method and other request details.

You need to specify the different attributes for the options object.
options can be an object or a string.

```
var postData = querystring.stringify({
'msg' : 'some arbitrary token'
});

var options = {
hostname: 'www.facebook.com',
port: 80,
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};

var req = http.request(options, (res) => {
console.log(STATUS: ${res.statusCode});
console.log(HEADERS: ${JSON.stringify(res.headers)});
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(BODY: ${chunk});
});
res.on('end', () => {
console.log('No more data in response.')
})
});

req.on('error', (e) => {
console.log(problem with request: ${e.message});
});

// write data to request body
req.write(postData);
req.end();```

NodeJS supports http.request as a standard module:

var http = require('http');

var options = {

  host: 'example.com',

  port: 80,

  path: '/foo.html'

};

http.get\(options, function\(resp\){

  resp.on\('data', function\(chunk\){

    //do something with chunk

  }\);

}\).on\("error", function\(e\){

  console.log\("Got error: " + e.message\);

}\);

Of course we can also combine node-http-proxy and express. node-http-proxy will support a proxy inside node.js web server via RoutingProxy (see the example called Proxy requests within another http server).

results matching ""

    No results matching ""