The nodejs write method of the nodejs built in file system module will come up a lot when it comes to do anything with, well writing a file in nodejs. There is more that one method in the file system module that can be used to write data to a file system, but this is the one that I find myself using all the time over the others when it comes to quick simple projects.
There is the old way of how to go about using the nodejs write file method that can lead to a kind of call back hell, and then there is the more modern way of using write file that involves the use of promises and the util promisify method. I generally choose to use promises over callbacks because I use write file along with many other methods and I find that promises are a much better way of making many calls to methods like this in a certain order.
In this section I will be going over a very basic hello world style example of the nodejs write file method. One that just involves the use of a callback that will work on a wide range of nodejs versions. I will then be going over a more advanced example that makes use of the promisify method in the util module that I can use to make a version of the write file method that returns a promise on versions of node that do not do so by default.
So this is just a getting started section of sorts with writing files in nodejs.
A basic example of the nodejs write file method is to require in the nodejs file system module, and then call the fs.wrietFile method passing the path of the file to write to as the first argument, followed by the data to write to, and then the encoding and callback to fire when done.
So in later versions of nodejs it looks like there might now be native support for Promises when using nodejs file system method like the write file method. However in this section I am assuming that you might be using an older version of node, or you might still want to support older versions of node for your project. So you just need to use some kind of dependency like fs-extra, or the util module promisify method to promisify the fs module or methods you want to use. One way or another you might still want to do something to make sure methods like fs.readFile and fs.writeFile will return promises one way or another in all versions of node that are concerned.
So here is an example where I am using the util promsiify method to make sure that the node write file method returns a promise. I am also doing so with the node read file methods also, and using the two methods to both read and write a simple json file.
When I call this example in the command line with node it will create a json file if it is not there, if it is there it will read the file step a value and then write it back to the file. As simple as it might be this basic node write file method example is the beginning of something that is starting to look like an actual node project examples of some kind.
So now that we have the basics of writing files in nodejs, as well as reading them lets do something fun. Here I worked out a very simple silly game example that has to do with what happens if the probability of winning a bet is even just one percent above fifty percent, and the bet remains low and constant. In the kind of situation of course you will make money over the long run if you play enough rounds.
This example involves a read state method that creates a state json file if it is not there, or just reads it, and returns the state file. In addition it also has a play round method that calls the read state method, plays a round of a game, updates the state, and then writes the updated state to the file.
When I run this a few times, and set the maxCount high enough over time sure enough the money property of the state does slowly but surly go up. The basic idea of this could be expanded to make all kinds of idle games where you just run it a bunch of times and then progress is made. If only real life was like that.