I am learning Nodejs with node cookbook 2nd edition.
I like this book because it is teaching us with explaining sample code which looks very practical.
Anyway the main question is that below is in the EX code
var http = require('http');
//We are synchronously loading form.htmlat initialization time instead of accessing the disk on each request.
var form = require('fs').readFileSync('form.html');
var maxData = 2 * 1024 * 1024; //2mb
var querystring = require('querystring');
var util = require('util');
http.createServer(function (request, response) {
if(request.hasOwnProperty('destroy')){console.log('!!!!!!')};
if (request.method === "GET") {
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(form);
}
if (request.method === "POST") {
var postData = '';
request.on('data', function (chunk) {
postData += chunk;
if (postData.length > maxData) {
postData = '';
// this?? stream.destroy();
this.destroy();
response.writeHead(413); // Request Entity Too Large
response.end('Too large');
}
}).on('end', function() {
if (!postData) { response.end(); return; } // prevents empty post which requests from, crash
var postDataObject = querystring.parse(postData);
console.log('User Posted:\n' + postData);
//inspect : for a simple way to output our postDataObjectto the browser.
response.end('You Posted:\n' + util.inspect(postDataObject));
});
}
}).listen(8080);
: This is HTTP server dealing with basic 'post' request from web user
over there, I do not figure out what is ( this.destroy(); ).
I guess that this is request readable streaming object. is not it? And I can understand what destroy() object is doing ( preventing any further data arriving from the client) but I cannot find destroy method at NodeJS API reference. (http://nodejs.org/api/all.html)
Can you explain me what is the this object and where is the destroy() method from?
In this context, this represents an instance of http.IncommingMessage which implements ReadableStream interface.
Apparently, destroy() is not documented in HTTP API reference but you can find it in the code: /lib/_http_incoming.js#L106-L112
Simply it destroys the socket associated with the Stream. You can find documentation of Socket.destroy() in Socket API.
Anyway, I think it would be better for you to use request.abort() as it's more "cleaner" when dealing with client request.