2013-03-18 86 views
2

我已經使用jQuery來將ajax發佈一些數據到nodejs web服務器。獲取帖子的有效載荷

Web服務器代碼收到帖子,但我不知道如何檢索有效負載,並且nodejs文檔網站太糟糕了。我試着將請求對象轉儲到調試器控制檯,但我無法在那裏看到數據。我如何訪問帖子的有效載荷?

文檔說request對象是http.IncomingMessage一個實例,並暴露了data事件與簽名function (chunk) { }其中一塊是字符串或Buffer,但如果你不知道在哪裏,你應該如何掛鉤直到這個事件,或者如何使用緩衝區,那麼這不是非常有用。


我注意到在「社區」而不是「文檔」鏈接下隱藏了第二個更具敘述性的手冊。這很好。它目前不可用。這不太好。


我有人問我是否使用框架或試圖做到「本地」。無知使我沒有直接回答,反而使這裏是現在我已經加入此

request.addListener('data', function(chunk){ 
    console.log('got a chunk'); 
}); 

爲createServer成功功能的啓動代碼

var http = require('http'); 
var fs = require('fs'); 
var sys = require('sys'); 
var formidable = require('formidable'); 
var util = require('util'); 
var URL = require('url'); 

var mimeMap = { htm : "text/html", css : "text/css", json : "application/json" }; 

var editTemplate = fs.readFileSync("edit.htm").toString(); 

http.createServer(function (request, response) { 

    request.addListener('data', function(chunk){ 
    console.log('got a chunk'); 
    }); 

    var body, token, value, mimeType; 
    var path = URL.parse(request.url).pathname; 

    console.log(request.method + " " + path); 

    switch (path) { 
    case "/getsettings": 
     try { 
     mimeType = "application/json"; 
     body = fs.readFileSync("/dummy.json"); 
     } catch(exception) { 
     console.log(exception.text); 
     body = exception; 
     } 
     //console.log(body.toString()); 
     break; 
    case "/setsettings": 
     console.log(request); //dump to debug console 
     //PROCESS POST HERE 
     body = ""; //empty response 
     break; 
    case "/": 
     path = "/default.htm"; 
     mimeType = "text/html"; 
    default: 
     try { 
     mimeType = mimeMap[path.substring(path.lastIndexOf('.') + 1)]; 
     if (mimeType) { 
     body = fs.readFileSync(path); 
     } else { 
      mimeType = "text/html"; 
      body = "<h1>Error</h1><body>Could not resolve mime type from file extension</body>"; 
     } 
     } catch (exception) { 
     mimeType = "text/html"; 
     body = "<h1>404 - not found</h1>"; 
     } 
     break; 
    } 
    response.writeHead(200, {'Content-Type': mimeType}); 
    response.writeHead(200, {'Cache-Control': 'no-cache'}); 
    response.writeHead(200, {'Pragma': 'no-cache'}); 
    response.end(body); 
}).listen(8124); 

console.log('Server running at http://127.0.0.1:8124/'); 

。它看起來像這樣工作,我想這意味着成功函數在聽衆之前被調用。如果這是而不是正確的地方綁定然後有人請告訴我。

+0

您是否正在使用某種類似Web的框架,或者正在嘗試以「原生」方式執行此操作? – 2013-03-18 23:09:50

+0

查看修改後的問題 – 2013-03-18 23:57:59

回答

2

在「// POST POST HERE」處添加一個req.pipe(process.stdout);它將輸出發佈數據到控制檯。

也可以將它管道到一個文件req.pipe(fs.createWriteStream(MYFILE))甚至回給瀏覽器req.pipe(res);

在這裏看到一個例子:http://runnable.com/nodejs/UTlPMl-f2W1TAABS

你也可以添加自己的處理程序的事件,數據,錯誤和結束

var body = ''; 

request.addListener('data', function(chunk){ 
    console.log('got a chunk'); 
    body += chunk; 
}); 

request.addListener('error', function(error){ 
    console.error('got a error', error); 
    next(err); 
}); 

request.addListener('end', function(chunk){ 
    console.log('ended'); 
    if (chunk) { 
    body += chunk; 
    } 
    console.log('full body', body); 
    res.end('I have your data, thanks'); 
}); 

哦,作爲對「本地或模塊」的問題,你可以使用像快遞將解析身體給你,並用結果填充req.body模塊(跳過addListe神經痛,甚至爲你解析formdata或json)。請參閱http://expressjs.com/api.html#req.body

+0

謝謝,這非常有幫助。正如你可以在我自己的示例代碼中看到的,我剛剛發現了處理程序的綁定,例如你所建議的,雖然我的數據很好地適合於一個塊,但我仍然很感激關於如何累積塊的解釋。你能否澄清*這些事件應該在哪裏宣佈?我是否在正確的位置? – 2013-03-19 01:39:42

+0

雖然你可能想要檢查'req.method ==='POST'',但是像express這樣的庫可以使app.post變得更容易,你有正確的位置標記爲「// POST POST HERE」 ('/ setSettings',function(req,res,next){console.log('body already parsed',req.body); next();});' – generalhenry 2013-03-19 02:11:55

+0

下面是一個例子:http://runnable.com /快遞/ UTlPPF-f2W1TAAET – generalhenry 2013-03-19 02:13:46