最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

NodeJS HTTP响应达到最大大小?

IT培训 admin 5浏览 0评论

NodeJS HTTP响应达到最大大小?

最近,我偶然发现了一个关于Node.JS REST API的非常奇怪的问题。目的是将HTTP请求转换为SQL请求,并将SQL响应转换为HTTP响应,这样我就可以通过SSL / TLS传输所有内容并对其进行编码。

问题是,当响应大小很小时,它都可以正常工作,但是当它超过某个大小(大约255.37 KB)时,响应主体会在JSON的中间切掉。经过多次测试后,问题似乎与HTTP响应总大小(包括标头)有关,因为删除某些自定义标头后,会发送更多的正文。我用PHP编写了类似的代码,并且来自PHP API的JSON响应很好,因此我认为问题是由Node.JS Web服务器中的错误引起的。另外,Content-Lenght标头还可以,并且具有正确的值(就像PHP API响应一样)。

我在Express的最新版本中使用Node.JS v6.11.0。感谢npm,我所有的依赖项都是最新的。

这里是处理GET HTTP请求并继续执行SELECT SQL请求,然后将答案解析为json字符串的函数代码

function SelectData(connection){
 return function (req, res) {
    let tableName 	 = req.params.table;
    let id 		     = req.params.id;

    console.log("GET request received with tableName = " + tableName + " and id = " + id);

    // Should match this regex
    if (!tableName.match(/^[\-\_\s0-9A-Za-z]+$/)){
      console.log("Error: Invalid table name provided : " + tableName);
    	badRequestError(req, res);
    	return;
    }
    if (id !== undefined){
      if (!id.match(/^[0-9]+$/)){
        console.log("Error: Invalid id provided : " + id + " (table name was valid : " + tableName + ")");
        badRequestError(req, res);
        return;
      }
    }
    // if id is empty, then don't use any condition, else, add SQL condition
    idPart = (id == undefined) ? "" : ('WHERE id="' + id + '"');
    // TODO: try to replace " + var + " by question marks and bind params, find a way to do it w/ id
    connection.query("SELECT * FROM " + tableName + " " + idPart + ";", function(err, result){
      res.setHeader('Content-Type', 'application/json');
      console.log("Request executed successfully !");
      // Works too but I prefer the .send() method
      // res.status(200).write(JSON.stringify(result)).end();
      res.status(200).send(JSON.stringify(result));
      req.connection.destroy();
      return;
    });
  }
}

function badRequestError(req, res){
  res.setHeader('Content-Type', 'text/plain');
  res.status(400).send("Bad Request");
  req.connection.destroy();
}

exports.SelectData = SelectData;
回答如下:

您是否找到了解决此问题的方法?我也有类似的情况。仅我发送媒体文件,由于某种原因,它们以多个数据包而不是一个数据包发送。

NodeJS HTTP响应达到最大大小?

最近,我偶然发现了一个关于Node.JS REST API的非常奇怪的问题。目的是将HTTP请求转换为SQL请求,并将SQL响应转换为HTTP响应,这样我就可以通过SSL / TLS传输所有内容并对其进行编码。

问题是,当响应大小很小时,它都可以正常工作,但是当它超过某个大小(大约255.37 KB)时,响应主体会在JSON的中间切掉。经过多次测试后,问题似乎与HTTP响应总大小(包括标头)有关,因为删除某些自定义标头后,会发送更多的正文。我用PHP编写了类似的代码,并且来自PHP API的JSON响应很好,因此我认为问题是由Node.JS Web服务器中的错误引起的。另外,Content-Lenght标头还可以,并且具有正确的值(就像PHP API响应一样)。

我在Express的最新版本中使用Node.JS v6.11.0。感谢npm,我所有的依赖项都是最新的。

这里是处理GET HTTP请求并继续执行SELECT SQL请求,然后将答案解析为json字符串的函数代码

function SelectData(connection){
 return function (req, res) {
    let tableName 	 = req.params.table;
    let id 		     = req.params.id;

    console.log("GET request received with tableName = " + tableName + " and id = " + id);

    // Should match this regex
    if (!tableName.match(/^[\-\_\s0-9A-Za-z]+$/)){
      console.log("Error: Invalid table name provided : " + tableName);
    	badRequestError(req, res);
    	return;
    }
    if (id !== undefined){
      if (!id.match(/^[0-9]+$/)){
        console.log("Error: Invalid id provided : " + id + " (table name was valid : " + tableName + ")");
        badRequestError(req, res);
        return;
      }
    }
    // if id is empty, then don't use any condition, else, add SQL condition
    idPart = (id == undefined) ? "" : ('WHERE id="' + id + '"');
    // TODO: try to replace " + var + " by question marks and bind params, find a way to do it w/ id
    connection.query("SELECT * FROM " + tableName + " " + idPart + ";", function(err, result){
      res.setHeader('Content-Type', 'application/json');
      console.log("Request executed successfully !");
      // Works too but I prefer the .send() method
      // res.status(200).write(JSON.stringify(result)).end();
      res.status(200).send(JSON.stringify(result));
      req.connection.destroy();
      return;
    });
  }
}

function badRequestError(req, res){
  res.setHeader('Content-Type', 'text/plain');
  res.status(400).send("Bad Request");
  req.connection.destroy();
}

exports.SelectData = SelectData;
回答如下:

您是否找到了解决此问题的方法?我也有类似的情况。仅我发送媒体文件,由于某种原因,它们以多个数据包而不是一个数据包发送。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论