读取node.js中的文本文件并处理行结尾

时间:2016-07-05 08:47:36

标签: node.js

我正在读取node.js中的文本文件,如下所示:

 var configfile = path.resolve(__dirname, file);
  
  fs.readFile(configfile, function (err, data) {
  if(err) {
    console.log(err);
  }
    var post_options = {
      'host': 'localhost',
      'port':'3001',
      'path': '/thefile',
      'method': 'POST',
      'headers': {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(data)
      }
    };

    // Set up the request
    var post_req = http.request(post_options, function(post_res) {
      post_res.on('data', function (chunk) {
        res.status(200);
        res.send(chunk);
      });
      post_res.on('end', function(){
        next();
        return;
      })
    });

    post_req.write(data.toString("utf8", 0, data.length));

我想要的是提供一个看起来像原始的文本文件。但是我看到行结尾变成了像/ n这样的实际文本,而不仅仅是被读作行结尾。我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

在这种情况下,我通过将数据类型切换为ascii而不是utf-8来解决问题。