Is there a way to refresh static files without server restart?

June 09, 2019, at 01:10 AM

My simple Node.js app serves static files, e.g. html/txt


I want to reflect my changes to those files without restarting the app. I.e. want to be able to modify any txt/help/ static files to it's available right away for the clients without restarting the app. How do I do it?

Answer 1

To determine if a file has changed, you can use from the built-in fs module. It works like this:'filename_or_directory', function(event, file) {}) . The callback's first parameter is "event", a string which is either "rename" or "change", depending on what type of change happened. The second parameter is the filename that changed.

For the client to automatically know that a change has occurred on the server is a bit more complex. There needs to be some form of communication with the server; you can do this with polling or with web sockets. If you go the socket route, you can use a library like or ws to establish a connection between server and client; when the server notices a change in a file from, it can broadcast that change as a JSON "event object" to all clients, which can then receive the message and determine how to proceed (reload the current page, request updated data via AJAX, ignore it because it's an unrelated file, etc.).

If you go the polling route, you don't need any web socket libraries. You'd just keep track of the timestamp of each change from in an array, then set up a route like /api/getChanges or something. You have the client, at regular intervals, post the timestamp of the last client update to that route, and the server can respond with all change objects in the array after that time.

Note: Express doesn't need to know that a file has changed. It will re-read files as they're requested. It's the client that needs to know when to refresh.

Answer 2

Fixed this way:

app.use(express.static('public’, {etag: false}))
Rent Charter Buses Company
Templating each array with Handlebars and Mongoose

Templating each array with Handlebars and Mongoose

I try to display results with Mongoose and Handlebars but I can'tIs this a problem withe the structure and the fact that the "results" arrays are strangely structured ? I want each object to be displayed in my page

MongoDB _id returns undefined on frontend (VueJS)

MongoDB _id returns undefined on frontend (VueJS)

I created a simple fullstack webapp using NodeJS & Express + MongoDB to build the backend API and VueJS for the frontend

Room name is not assigned correctly in Socket.IO

Room name is not assigned correctly in Socket.IO

Upon a button being pressed client-side, a socketemit is called with the parameter being defined by an input box

Mongoose is not connecting MongoDB Atlas

Mongoose is not connecting MongoDB Atlas

This is the first time I use MongoDB Atlas to work with Mongo, and while trying to connect, that's the error I get: