JSON, Timers and CLI Tools¶
Working with JSON¶
JSON (JavaScript Object Notation) is commonly used for configuration, data files, and APIs.
Read JSON from a File¶
const fs = require("fs");
const data = fs.readFileSync("data.json", "utf8");
const parsed = JSON.parse(data);
console.log(parsed.name);
Write JSON to a File¶
const user = { name: "Alice", age: 25 };
const json = JSON.stringify(user, null, 2); // pretty print
fs.writeFileSync("user.json", json);
Parse and Stringify¶
const obj = JSON.parse('{"name": "Bob"}');
const str = JSON.stringify(obj);
Timers¶
Node provides timer functions similar to the browser.
setTimeout¶
Run a function once after a delay (ms).
setTimeout(() => {
console.log("Delayed message");
}, 1000);
setInterval¶
Run a function repeatedly at an interval (ms).
const intervalId = setInterval(() => {
console.log("Repeats every 2s");
}, 2000);
setTimeout(() => clearInterval(intervalId), 7000); // stop after 7s
setImmediate¶
Runs a callback after the current event loop phase.
setImmediate(() => {
console.log("Runs immediately after current task");
});
clearTimeout / clearInterval¶
Stop scheduled timers.
const id = setTimeout(() => {}, 5000);
clearTimeout(id);
Building CLI Tools¶
Reading Command-Line Arguments¶
const args = process.argv.slice(2);
console.log(args); // ["hello", "world"]
Run with:
node script.js hello world
Simple Flag Parser¶
const flags = {};
process.argv.slice(2).forEach(arg => {
const [key, value] = arg.split("=");
flags[key] = value;
});
console.log(flags);
// node app.js name=alice age=30
// Output: { name: 'alice', age: '30' }
Output with process.stdout¶
process.stdout.write("Type something: ");
Input with process.stdin¶
process.stdin.on("data", (data) => {
console.log(`You typed: ${data.toString().trim()}`);
});
Summary¶
Concept | Method/Function |
---|---|
Read JSON | fs.readFileSync + JSON.parse() |
Write JSON | JSON.stringify() + fs.writeFileSync |
Delayed Run | setTimeout() |
Repeated Run | setInterval() |
CLI Args | process.argv |
Read from stdin | process.stdin.on("data") |