2014-04-29 201 views
0

我有以下代码response.writeHead不发送POST数据

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代码(减去控制台),从哪里开始是起始页面,上传返回什么(以前的代码)

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; 
+1

您能告诉我们一个可重复的示例代码片段吗?从你所展示的看来,它看起来应该很好。 – loganfsmyth

+0

就是这样,代码段应该可以工作。我遵循并最终从教程中复制并粘贴了整个文件。所以我认为这不是一个代码问题,而是一个配置问题,或者在我的数据中。完整的requestHandler文件看起来像我在最后发布的额外位。 – Blue

+0

感谢您的帮助,我掏空了所有的文件,只是复制粘贴。之后,我回去发现我在主服务器文件中留下了一条额外的路线。我的猜测是,它发送的响应,然后与记录器运行的功能。所以上面的代码正在运行,但响应实际上没有使用。 – Blue

回答

1

我认为错误可能不是你的requestHandlers.js。尽管在你的代码片段中,你忘记了在“response.write(”你发送了文本:“+ stuff);”行,我假设这是一个复制和粘贴错误。

你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; 

注意的路由被称为在请求对象“结束”处理程序和POSTDATA在请求对象的建立在“块”“数据“处理程序。

在你的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页。

+0

谢谢你,根据你的建议(和复制粘贴评论是正确的)我回去了我的代码,并最终全部重新。它原来不是postData变量,但我有一个额外的响应电话,所以我正在记录的一个正在运行,然后被忽略(我认为)。 – Blue

+0

@ user393001 - 如果你很高兴:)你能标记我的答案解决吗? –

0

另一个解决方法是删除函数末尾的“.text”。我正在使用相同的教程,并遇到同样的问题。我没有正确地按照教程100%,但我发现删除了“.text”。并且字符串化将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(); 
}