Node.js文件系统没有正确写入换行符

时间:2017-12-29 17:57:27

标签: javascript node.js express

我允许用户在pre标签内编写代码,然后将pre标签中的任何内容保存到磁盘上的文件中。我在node / express中使用以下内容:

fs = require('fs')

fs.writeFile("path_to_file", "string_of_code", 'utf8', function(err) {

})

这会使用pre标记(string_of_code)中的代码将新文件写入磁盘。但它没有正确保存文件。未捕获预标记中的换行符:

enter image description here

黑色屏幕是预标签,白色屏幕是保存的文件。我尝试过编码code_string,然后替换

"<br>" 

"\n" 

在将code_string传递给节点之前,但新行仍未被fs捕获:

code_string = encodeURIComponent(strip($('#hold_code_editor').html().replace(/<br>/g, '\n')));

这会转换为:

import matplotlib.pyplot as plt

class data_gather_explore:

    def gather_data(num):
        data = datasets.load_boston()        
        res = 1 + 2
        return(data)

到此:

mport%20matplotlib.pyplot%20as%20plt%0A%0Aclass%20data_gather_explore%3A%0A%0A%20%20%20%20def%20gather_data(num)%3A%0A%20%20%20%20%20%20%20%20data%20%3D%20datasets.load_boston()%20%20%20%20%20%20%20%C2%A0%20%20%20%20%20%20%20%20res%20%3D%201%20%2B%202%0A%20%20%20%20%20%20%20%20return(data)%0A%0A

0 个答案:

没有答案
相关问题