2014-04-29 121 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(); 
}