09 / 16

How can you append data to an existing file in Node.js?

Difficulty: 6/10
File System (fs), Streams & I/O, Asynchronous JS

You can append data to an existing file in Node.js using the fs.appendFile() method. Here's an example:

javascript

Scenario Questions

0-2 years experience

  1. 1We have a simple Express route that needs to log incoming request IPs to a local 'access.log' file. How would you write the code to append each IP on a new line without overwriting the existing logs, and how would you handle it if the file doesn't exist yet?
  2. 2Imagine you used fs.writeFileSync to add a new user's email to a 'subscribers.txt' file, but you realized it keeps wiping out all the previous subscribers. What went wrong, and how would you fix it using the fs module?

2-5 years experience

  1. 1We are building a feature that exports a large database table to a CSV file. A junior developer suggested using fs.appendFile inside a forEach loop for every row. Why might this cause memory issues or performance bottlenecks, and what approach would you take instead?
  2. 2Suppose you have an Express server with multiple concurrent API requests trying to append payment transaction logs to the same 'transactions.log' file at the exact same time. What issues could arise with file locking or data corruption, and how would you ensure the logs don't get garbled?

5-8 years experience

  1. 1We have a high-throughput Node.js microservice processing 5,000 events per second. We need to persist these events to a local disk buffer before forwarding them. How would you design the file-writing layer to handle backpressure, avoid blocking the event loop, and ensure we don't run out of memory?
  2. 2During a peak traffic event, our Node.js application crashed because the disk filled up while appending to an audit log. The crash left the last log entry partially written and corrupted. How would you refactor the file-appending logic to handle disk-full errors gracefully and prevent log corruption?

8+ years experience

  1. 1Our legacy monolith currently appends application logs directly to the local EBS volume using a custom Node.js stream wrapper. As we migrate to an auto-scaling Kubernetes cluster, this approach is no longer viable. How would you architect the transition from local file appending to a distributed logging solution, and what are the trade-offs regarding latency, cost, and reliability?
  2. 2You are designing a shared logging library to be used by 50+ internal Node.js microservices. Some teams require synchronous local file appending for compliance, while others need high-throughput async streaming. How would you design the abstraction layer to support these diverse requirements while preventing common pitfalls like event loop blocking and memory leaks across the organization?

Follow-up Questions

  • What is the difference in memory overhead between fs.appendFile and a WriteStream with the 'a' flag?
  • How does Node.js handle backpressure when writing to a stream?
  • What happens to the event loop if you use synchronous file append methods under high load?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.