Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取邮政有效载荷_Node.js_Jquery - Fatal编程技术网

Node.js 获取邮政有效载荷

Node.js 获取邮政有效载荷,node.js,jquery,Node.js,Jquery,我使用jQuery将一些数据发布到nodejs Web服务器 web服务器代码接收帖子,但我不知道如何检索有效负载,而nodejs文档web非常糟糕。我已尝试将请求对象转储到调试器控制台,但在那里看不到数据。如何访问post的有效负载 文档中说请求对象是http.IncomingMessage的一个实例,并公开了一个带有签名的函数(chunk){}的数据事件,其中chunk是字符串或缓冲区,但是如果您不知道应该在哪里或如何连接到该事件,或者如何使用缓冲区,这并没有太大帮助 我注意到,在“社区”

我使用jQuery将一些数据发布到nodejs Web服务器

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

文档中说请求对象是http.IncomingMessage的一个实例,并公开了一个带有签名的
函数(chunk){}
数据事件,其中chunk是字符串或
缓冲区
,但是如果您不知道应该在哪里或如何连接到该事件,或者如何使用缓冲区,这并没有太大帮助


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


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

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/');
指向createServer的成功函数的开始。它似乎是这样工作的,我想这意味着success函数是在侦听器之前调用的。如果这不是正确的绑定位置,请有人告诉我。

在“//PROCESS POST HERE”添加一个
请求管道(PROCESS.stdout)将post数据输出到控制台

您还可以通过管道将其传输到文件
req.pipe(fs.createWriteStream(MYFILE))
或甚至返回到浏览器
req.pipe(res)

请参见此处的示例:

您还可以向事件、数据、错误和结束添加自己的处理程序

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 or module”问题,您可以使用像express这样的模块为您解析主体,并用结果填充req.body(跳过addListener的痛苦,甚至为您解析formdata或json)。请参见

您是在使用express之类的web框架,还是在尝试以“本地”方式进行此操作?谢谢,这非常有帮助。正如您在我自己的示例代码中所看到的,我刚刚发现了处理程序的绑定,如您所建议的,尽管我的数据非常适合于一个块,但我仍然非常感谢您对如何累积块的解释。你能澄清一下这些事件应该在哪里宣布吗?我是在正确的位置做的吗?您在正确的位置标记了“//PROCESS POST HERE”,虽然您可能还想检查
req.method==='POST'
,但是像express这样的库会使
app.POST('/setSettings',function(req,res,next){console.log('body已经解析,req.body);next();})更容易实现这一点
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');
});