Jorge Valle

Jorge Valle

Jorge Valle is a front end developer with a particular passion for, and expertise in, JavaScript and user interfaces. Lately, he's also been diving into machine learning.

Writing a JSON file in Node.js

Writing to files in Node.js is easily accomplished through the file system API.

Recently, I ran into a situation where I needed to write various config files to disk as part of the CMS I am building. More specifically, I needed to write JSON from a form that is captured in the admin section of the CMS.

A good way to do this is to construct the JSON in your controller, and then once built, write it to the file in one operation.

The controller then, looks like this.

Once written, you can also use the file system API to read through the file to either use these settings directly in your application, or store them in the database at a later point.

Panoramic view of the port of Rijeka, Croatia's largest