response.writeHead不发送帖子数据

时间:2014-04-29 05:37:24

标签: node.js response

我有以下代码

console.log(typeof(postData));
console.log(typeof(querystring.parse(postData)));
console.log(typeof(querystring.parse(postData).text));
console.log(querystring.parse(postData).text);
var stuff = querystring.parse(postData).text;
console.log(stuff);
stuff = "You've sent the text: " + stuff;
console.log(stuff);
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
console.log(response.write(stuff));
response.end();
console.log("More text");
console.log(stuff);

在控制台中我得到了

string
object
string
ffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffff
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff
Request handler 'upload' was called.
false
More text
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff

但是在我看到的网页上

You've sent the text: undefined

我无法弄清楚为什么我的帖子数据没有发送,但我追加的字符串是。

我正在关注The Node Beginner Book的教程。

完整的requestHandlers代码(减去控制台),其中start是起始页面,上传是返回的内容(上一个代码)

var querystring = require("querystring");
function start(response, postData) {
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) {
    var stuff = querystring.parse(postData).text;
    stuff = "You've sent the text: " + stuff;
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end();
}
exports.start = start;
exports.upload = upload;

2 个答案:

答案 0 :(得分:1)

我认为错误可能不在您的requestHandlers.js中。虽然,在您的代码段中,您已经忘记输入&#34; response.write(&#34;您已发送文字:&#34; +内容);&#34;我认为这是一个复制和粘贴错误。

您的start.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 for " + pathname + " received.");
        request.setEncoding("utf8");

        request.addListener("data", function (postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk '" + postDataChunk + "'.");
        });

        request.addListener("end", function () {
            route(handle, pathname, response, postData);
        });

    }

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

exports.start = start;

请注意,路径在请求对象中被调用&#34; end&#34; handler和postData是在&#34; chunks&#34;中构建的。在请求对象&#34;数据&#34;处理程序。

在你的router.js文件中应该是;

function route(handle, pathname, response, postData) {
    console.log("About to route a request for " + pathname);
    if (typeof handle[pathname] === 'function') {
        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;

我认为这里的关键是确保传递postData变量而不是请求变量。我猜这是你的代码中发生的事情。

这一切都在本书的第51-53页。

答案 1 :(得分:0)

另一种解决方法是删除函数末尾的“.text”。我使用相同的教程,遇到了同样的问题。我没有正确地遵循教程100%,但我发现删除了“.text”。和stringifying将postData返回到控制台和服务器网页。

function enterUsers(response, postData)
{
console.log("user entered");
//response.write(query.fullname); 
//response.write("\nuser entered... ;)");
//response.write(postData); 
//response.write(querystring.parse(postData).text)

response.write("\n"+postData)
var receivedData = querystring.parse(postData);
console.log(receivedData); 
response.write("\nYou've sent the text: \n" + querystring.stringify(receivedData, '\n', ' = '));
response.end();
}