node 写入文件
The easiest way to write to files in Node.js is to use the fs.writeFile()
API.
在Node.js中写入文件的最简单方法是使用fs.writeFile()
API。
Example:
例:
const fs = require('fs')
const content = 'Some content!'
fs.writeFile('/Users/flavio/test.txt', content, (err) => {
if (err) {
console.error(err)
return
}
//file written successfully
})
Alternatively, you can use the synchronous version fs.writeFileSync()
:
另外,您可以使用同步版本fs.writeFileSync()
:
const fs = require('fs')
const content = 'Some content!'
try {
const data = fs.writeFileSync('/Users/flavio/test.txt', content)
//file written successfully
} catch (er