4

我有一个如下定义的简单函数

function upload(response, postData) {
    console.log("Received: " + postData);
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("You've sent text: " + querystring.parse(postData).text);
    response.end();
}

当我将数据登录到控制台时,它会正确显示,但在写入响应对象时显示为“未定义”。为什么会发生这种情况?我的代码有什么错误吗?

整个代码

路由.js

function route(handle, pathname, response, postData) {
    console.log("About to route a request for " + pathname);
    if(typeof(handle[pathname]) === 'function') {
        console.log(postData);
        handle[pathname](response, postData);
    } else {
        console.log("no request handler found for " + pathname);
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("404 Not found");
        response.end();
    }
}

exports.route = route;

requestHandler.js

var exec = require("child_process").exec;
var querystring = require("querystring");

function start(response, postData) {
    console.log("request handler 'start' was called");
    var body = '<html>' +
    '<head>' +
        '<meta http-equiv="Content-Type" content="text/html; ' +
        'charset=UTF-8" />' +
    '</head>'+
    '<body>'+
        '<form action="/upload" method="post">'+
            '<textarea name="text" rows="20" cols="60"></textarea>'+
            '<input type="submit" value="Submit text" />'+
        '</form>'+
    '</body>' + '</html>';

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

function upload(response, postData) {
    console.log("Received: " + querystring.parse(postData).text);
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("You've sent text: " + querystring.parse(postData).text);
    response.end();
}

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

服务器.js

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

function start(route,handle) {

    function onRequest(request,response) {
        var postData = "";
        var pathname = url.parse(request.url).pathname;
        console.log("Request received for " + pathname + " received.");
        request.setEncoding("utf8");
        request.addListener("data", function(postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk '" + postDataChunk + "'.");
            });
        request.addListener("end", function() {
            console.log(postData);
            route(handle, pathname, response, postData);
            });
        route(handle,pathname,response);
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started...");
}

exports.start = start;
4

3 回答 3

1

代码在有和没有的情况下执行两次postData。谢谢莱纳斯 G 泰尔。在发布之前,我应该更清楚地了解代码。

于 2012-12-22T12:25:47.670 回答
1

我认为这是一个错字 - 使用

querystring.parse(postData).undefinedtext 

用于 requestHandler.js 中的上传功能

我正在学习相同的教程,也被困在这里。

于 2013-11-25T21:52:55.390 回答
0

这显然有点晚了,但我和你有同样的错误,但我没有执行两次路由。我的错误是我的 postData 变量未定义,因此它给出了

undefinedtext=sometext+querttest 

这个问题很容易解决我初始化你的postData =“”。希望这可以帮助任何被困在这里的未来访客。

于 2015-08-09T03:57:14.060 回答