How to replace all the broken images with newly updated images using node.js?

954 Views Asked by At

This is not a question asking how to replace all the broken images with the default images. That question is answered here.

What I want to know is how to replace all the broken images with newly updated images. On my web chat application, users can update their profile pictures and the comments that are posted after the update will have the new pictures shown next to them. However, the new pictures won't be shown next to old comments that are posted before the update.

For example, when you change your profile picture on twitter, not only your profile picture will be updated, but also pictures beside your old tweets will be changed into the new pictures. This is what I want to do.

Does anyone know how to do that with node.js?

2

There are 2 best solutions below

0
On

What @RajeshDan wrote is propably your best bet, use your fs module to update a user's image, while keeping both file-path and -name.

If you have your profile-images in a folder ./public/images/profile/ you can update one with a form, that posts the image and the users unique ID (hopefully that's acutally unique).

fs.writeFile('./public/images/profile/'+userID+'.png', userImage, (err) => {
  if (err) throw err;
  console.log('New image for user with id ' + userID);
});

this also adds the image if there hasn't been one at that point

0
On

Overwrite the old profile picture with the new profile picture with the same name as the old picture.

That way, you don't have to change img src and all the old pictures become new pictures.