Skip to content

Instantly share code, notes, and snippets.

@ryotah
Last active August 18, 2020 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryotah/4e1f9b097a93acd25bb8b90c12699a5f to your computer and use it in GitHub Desktop.
Save ryotah/4e1f9b097a93acd25bb8b90c12699a5f to your computer and use it in GitHub Desktop.
Node.js Read/Write
// READ
// node read.js < 1596859367268.json
const data = require("fs").readFileSync("/dev/stdin", "utf8");
console.log(data);
// WRITE
// node foo.js > `date '+%Y%m%d%H%M%S'`.json 
// or
// node write.js
const filename = `${String(new Date().valueOf())}.json`;
const data = JSON.stringify(process.env);
require("fs").writeFile(
  require("path").resolve(__dirname, filename),
  data,
  (err) => {
    if (err) throw err;
  }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment