在 Node.js 中编写文件 [英] Writing files in Node.js

查看:46
本文介绍了在 Node.js 中编写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试寻找一种在使用 Node.js 时写入文件的方法,但没有成功.我该怎么做?

I've been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

推荐答案

文件系统 API.最常见的方式是:

const fs = require('fs');

fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }
    console.log("The file was saved!");
}); 

// Or
fs.writeFileSync('/tmp/test-sync', 'Hey there!');

这篇关于在 Node.js 中编写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆