Owow
Owow

Reputation: 347

fs.writeFile while clients try to get the file

I'm doing image modifications on a .png and then writing it on my server with

fs.writeFile

Problem is if some clients try to http get the .png from my server while i'm writing it, it will fail.

How can i prevent that ?

Upvotes: 0

Views: 72

Answers (2)

Marin Bînzari
Marin Bînzari

Reputation: 5368

Save the modified files under a diferent name then rename it to the original name. The rename operation is an atomic one so it will be done instantly. Steps:

  1. Make the modification of original.png and write them to original.png.mod
  2. Rename original.png.mod in original.png

Upvotes: 3

Quentin
Quentin

Reputation: 944568

Say you are currently writing to example.png.

Write to a different file, and then move it to replace example.png.

That removes the lag between opening the file for writing and finishing writing to it.

Upvotes: 1

Related Questions