Node.js request data event not firing. What am I d

2020-07-27 04:17发布

问题:

It seems like the data event on the request object is not firing, or I can't wire it up right in order to get anything from it. I am getting the end event just fine, and all the urls are working. I have looked on forums and documentation, and when I look at my code it seems like it should work. I am using Node version 0.10.12.

I am new. I am trying to get a simple server going in node. I was following The Node Beginner Book. Most of this code comes from there, with some very small modifications. I have already tried the code straight from the book.

Here is the code I am working with now.

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;

Thanks in advance.

回答1:

You need to add your data listener before you call route. Reason being, in your handlers you are calling response.end. When you do this, node checks if there are any data listeners on the socket, and if not, it will dump the existing request data, as it figures it's no longer needed.