Node.js请求数据事件未触发。我究竟做错了什么?

时间:2014-03-24 00:47:57

标签: javascript node.js events request

似乎请求对象上的数据事件没有触发,或者我无法正确连接它以便从中获取任何内容。我收到了最终结果,并且所有网址都正常运行。我查看了论坛和文档,当我查看我的代码时,它似乎应该可以工作。我使用的是Node版本0.10.12。

我是新人。我想让一个简单的服务器进入节点。我正在关注The Node Beginner Book。大多数代码来自那里,只有一些非常小的修改。我已经直接从书中试过了这段代码。

以下是我正在使用的代码。

index.js

var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");

var handle = {};

handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

if (process.argv[2] !== undefined && process.argv[2] !== null) {
    server.start(router.route, handle, process.argv[2]);
} else {
    server.start(router.route, handle);
}

router.js

route = function(handle, pathname, response, postData) {
    if (typeof handle[pathname] === "function") {
        handle[pathname](response, postData);
    } else {
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.end("404 Not found");
    }
}

exports.route = route;

server.js

var http = require('http');
var url = require('url');

var portToUse = 8888;

start = function(route, handle, port) {
    if (port !== undefined && port !== null && typeof parseInt(port) === 'number' && port > 1000 && port < 10000) {
        console.log('You passed in the port number %d.', port);
        portToUse = port;
    }

    http.createServer(function(request, response) {
        var postData = '';
        var pathname = url.parse(request.url).pathname;
        console.log('Request for %s received.', pathname);

        route(handle, pathname, response);

        request.setEncoding('utf8');

        // I have tried both .on and .addListener
        request.on('data', function(postDataChunk) {
            postData += postDataChunk;
            console.log('Received POST data chunk %s.', postData);
        });

        request.on('end', function(postDataChunk) {
            console.log(postDataChunk);
            route(handle, pathname, response, postData);
        });
    }).listen(portToUse);

    console.log('The server listening on %d.', portToUse);
}

exports.start = start;

requestHandlers.js

var exec = require('child_process').exec;

start = function(response, postData) {
    var body = '<!doctype html>' + 
    '<html lang="en">' + 
    '<head>' + 
    '<meta charset="UTF-8">' + 
    '<title>Upload Server</title>' + 
    '</head>' + 
    '<body>' + 
    '<form action="/upload">' + 
    '<textarea name="text" id="text-area" cols="40" rows="20"></textarea>' + 
    '<input type="submit" value="Submit text">' + 
    '</form>' + 
    '</body>' + 
    '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.end(body);
}

upload = function(response, postData) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('You\'ve sent: ' + postData);
}

exports.start = start;
exports.upload = upload;

提前致谢。

1 个答案:

答案 0 :(得分:1)

在致电data之前,您需要添加route听众。原因是,在您的处理程序中,您正在调用response.end。执行此操作时,节点检查套接字上是否有任何数据侦听器,如果没有,它将转储现有的请求数据,因为它表示不再需要它。