node.js - 获取帖子的有效载荷

标签 node.js jquery

我使用 jQuery 将一些数据通过 ajax 发送到 nodejs 网络服务器。

网络服务器代码收到了帖子,但我不知道如何检索有效载荷,而且 nodejs 文档网络非常糟糕。我试过将请求对象转储到调试器控制台,但我看不到那里的数据。如何访问帖子的负载?

文档说请求对象是 http.IncomingMessage 的一个实例,并公开了一个带有签名 function (chunk) { }data 事件> 其中 chunk 是字符串或 Buffer,但如果您不知道应该在哪里或如何连接到此事件,或者如何使用 Buffer,那么这不是非常有帮助。


我注意到在“社区”而不是“文档”链接下隐藏了第二本更具叙述性的手册。这很好。目前不可用。这不太好。


有人问我是在使用框架还是尝试“原生”。无知使我无法直接回答,所以这里是代码

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/');

现在我已经添加了这个

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

到 createServer 成功函数的开始。它似乎是这样工作的,我想这意味着成功函数在听众之前被调用。如果这不是正确的绑定(bind)位置,请告诉我。

最佳答案

在“//PROCESS 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');
});

哦,关于“ native 或模块”问题,您可以使用像 express 这样的模块,它会为您解析主体,并用结果填充 req.body(跳过 addListener 的痛苦,甚至解析 formdata 或 json你)。参见 http://expressjs.com/api.html#req.body

关于node.js - 获取帖子的有效载荷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15488558/

相关文章:

node.js - 全局安装本地开发的 npm 包

node.js - 如何编写云函数以使用 SendGrid 发送电子邮件

MySQL Node JS .then() 不是函数

node.js - Socket.io 1.0 - 事件仅广播到房间,我想发送给所有人(包括发件人)

javascript - 选择的 jquery 插件基本 ui 不起作用

javascript - jQuery如何在每个分号后插入<br/>标签

JavaScript:创建具有不同名称的表单字段?

javascript - 有没有办法让 Apache Bench 测试提供给 URL 的所有资源的响应时间?

javascript - 如何让我的 content2 边框进入 content1 边框半径内以及如何在 content2 元素之间放置一条垂直线?

jquery - 如何将 AJAX 数组响应推送到 js 数组中