97
93

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.js でファイルを保存する方法

Last updated at Posted at 2014-10-09

ちょっとした備忘録♪

テキストファイルを保存

text の内容を保存して hoge.txt を作るスクリプト.

script
var fs = require('fs');

var text = "hoge foo bar";
fs.writeFile('hoge.txt', text);

出力結果

hoge.txt
hoge foo bar

json ファイルを保存

json の内容を保存して hoge.json を作るスクリプト

var fs = require('fs');

var data = {
    hoge: 100,
    foo: 'a',
    bar: true,
};
fs.writeFile('hoge.json', JSON.stringify(data, null, '    '));

出力結果

hoge.json
{
    "hoge": 100,
    "foo": "a",
    "bar": true
}

JSON.stringify(data) でも良いんですが
出力後のファイルを見やすくするために JSON.stringify(data, '', ' ') としています.

97
93
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
97
93

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?