serve content from file vs database in node serve content from file vs database in node json json

serve content from file vs database in node


Why add another layer of indirection? Just serve the views straight from JSON.


Normally, database is used for serving dynamic content that changes frequently, records have one-to-many or many-to-many relationships, and you need to query the data based on various criteria.

In the case you described, it looks like you will be OK with JSON file cached in server memory. Just make sure you update the cache whenever content of the file changes, i.e. by restarting the server, triggering cache update via http request or monitoring the file at the file system level.

Aside from that, you should consider caching of static files on the server and on the browser for better performance

  1. Cache and Gzip static files (html,js,css,jpg) in server memory on startup. This can be easily done using npm package like connect-static
  2. Use browser cache of the client by setting proper response headers. One way to do it is adding maxAge header on the Express route definition, i.e:

app.use "/bower", express.static("bower-components", {maxAge: 31536000})

Here is a good article about browser caching


If you are already storing your views as JSON and using Node, it may be worth considering using a MEAN stack (MongoDB, Express, Angular, Node):

This way you can code the whole thing in JS, including the document store in the MongoDB. I should point out I haven't used MEAN myself.

MySQL can store and serve JSON no problem, but as it doesn't parse it, it's very inflexible unless you split it out into components and indexing within the document is close to impossible.

Whether you 'should' do this depends entirely on your individual project and whether it is/how it is likely to evolve.

As you are implementing a new version (with CMS) of the website it would suggest that it is live and subject to growth or change and perhaps storing JSON in MySQL is storing up problems for the future. If it really is just one file, pulling from the file system and caching it in RAM is probably easier for now.

I have stored JSON in MySQL for our projects before, and in all but a few niche cases ended up splitting up the component data.