goddyZhao / nproxy

A cli proxy tool specialized in file replacing
http://goddyzhao.me/nproxy
MIT License
511 stars 88 forks source link

TypeError: first argument must be a string or Buffer #21

Open netwjx opened 11 years ago

netwjx commented 11 years ago
TypeError: first argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.write (http.js:768:11)
    at Object.utils.request (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/utils.js:142:15)
    at Object.respondFromWebFile (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/responders/web-file.js:4:10)
    at Object.respond [as handle] (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/respond.js:72:24)
    at next (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/proto.js:198:3)
    at app (/opt/node-v0.10.4/lib/node_modules/nproxy/node_modules/connect/lib/connect.js:65:37)
    at Server.<anonymous> (/opt/node-v0.10.4/lib/node_modules/nproxy/lib/nproxy.js:49:5)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2015:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:119:23)

错误发生在使用POST提交表单时,我看了下代码

/opt/node-v0.10.4/lib/node_modules/nproxy/lib/utils.js:47

  /**
   * Simple wrapper for the default http.request
   *
   * @param {Object} options options about url, method and headers
   * @param {Function} callback callback to handle the response object
   */
  request: function(options, callback){
    //.......
    if(requestMethod === 'POST'){
      request.write(options.data);
    }
    //.......
  }

调用来源于/opt/node-v0.10.4/lib/node_modules/nproxy/lib/middlewares/responders/web-file.js:4

var utils = require('../../utils');

function respondFromWebFile(filePath, req, res, next){
   utils.request({
      url: filePath,
      method: req.method,
      headers: req.headers
    }, function(err, data, proxyRes){
      if(err){ throw err; }
      res.writeHead(200, proxyRes.headers);
      res.write(data);
      res.end();

    });
};

module.exports = respondFromWebFile;

对于POST请求没有传data

xiaojue commented 10 years ago

我也遇到了相同的问题,作者修复一下吧。

jkpulipati commented 9 years ago

Hi Please let me know what's wrong in this me also facing same issue, "TypeError: first argument must be a string or Buffer"

app.get('/', function (req, res) {

var postData = querystring.stringify(coreRequest.coreRequestFormat());
// Here coreRequest.coreRequestFormat() it will return JSON Object.
var options = {
    host: '192.168.1.86',
    port: 8080,
    path: '/coresoft-system-4.3.7/services/rest/handleRequest',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': postData
    }
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body req.write(postData); req.end(); }