Node.js请求数据事件未触发.我究竟做错了什么? [英] Node.js request data event not firing. What am I doing wrong?

查看:82
本文介绍了Node.js请求数据事件未触发.我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎请求对象上的数据事件未触发,或者我无法正确连接它以便从中获取任何信息.我得到的结束事件很好,并且所有的网址都在工作.我看过论坛和文档,当我看我的代码时,它似乎应该工作.我正在使用Node版本0.10.12.

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

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

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

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;

谢谢.

推荐答案

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

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.

这篇关于Node.js请求数据事件未触发.我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆