2013-03-18 71 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