nodejs这个属性和destroy()方法



我正在学习Nodejs与节点食谱第二版。

我喜欢这本书,因为它教我们解释示例代码,看起来非常实用。

无论如何,主要问题是以下是在EX代码

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);

:这是HTTP服务器,处理来自网络用户的基本"发布"请求

在那里,我不知道是什么(this.destroy();)。

我想这是一个请求可读的流对象。不是吗?我能理解destroy()对象在做什么(阻止来自客户端的任何进一步的数据),但是在NodeJS API引用中找不到销毁方法。(http://nodejs.org/api/all.html)

你能解释一下这个对象是什么吗?destroy()方法来自哪里?

在此上下文中,this表示http的一个实例。实现ReadableStream接口的IncomingMessage。

显然,destroy()没有记录在HTTP API参考中,但您可以在代码/lib/_HTTP_incoming.js#L106-L112 中找到它

简单地说,它破坏了与Stream相关联的套接字。您可以在Socket API中找到Socket.destroy()的文档。

无论如何,我认为使用request.abort()会更好,因为它在处理客户端请求时更"干净"。