2016-07-05 66 views
0

我读在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这样的实际文本,而不仅仅是作为行结尾读取。我不知道如何解决这个问题。

+0

你的意思是用
替换\ n吗? –

回答

0

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