So in this basic script here, just output something in the console.
Now how about writing this data to a file and then reading the data in from a file?
For that, we need a couple of packages which are available when you install Node.js. Node.js works such
that certain, very important features are available globally, for example setTimeout.
This is a browser API that was also implemented in Node.js and you can just use it like this in
any Node.js file but other functionalities which you don't need that often need to be imported explicitly,
for example functionalities for working with the filesystem, for that Node.js works with so-called
modules and on nodejs.org, you can learn more about that. In the docs there,
if you click on API reference, you'll find all the packages, all the core modules Node.js is made up
of and
for example here under f you find the filesystem module and you find the complete documentation for this
module.
Now as you can tell, there's a lot in there and of course we'll just briefly dive into this but we will
use it a bit at least.
So there for example we see how we can make this module and its features available in a Node.js script
and that's one important difference to browser side scripts,
the Node.js import export syntax looks a bit different,
instead of using the import and export keywords, in Node.js you use require to require a module, be
that a module provided by Node.js
like in this example or be that your own file and then you store the required data in your own constant
which you can then use in your script and I will later also show you how you export something.
So with that, let's copy that and add this here in our app.js file and this pulls in the filesystem module
which again was already installed as part of Node.js, so we don't need to install it separately but
we still need to add it to this file with this line and now we can use the filesystem object here to
work with the filesystem, for example to call the write file method here which takes a path to a file
and then the data we want to write to that file and there we could, as a simple starting point, just provide
a file name and then this will be written in the same path as the script sits in,
for example we could write this to user-data.txt let's say, to write a text file and then
as a data, we could have some text here,
user name equals Max for example. You can also defined some write options here as a third argument where
you can set things like the encoding and so on but we don't need this here, we can just execute this
file like this.
Now write file then takes a callback as a third argument which potentially holds an error if it failed
and we can then check if we do have an error, in which case we could console log it for example and otherwise
we know we succeeded and we could console log wrote to file and now with that if we now execute our app.js
file, you see wrote to file here and you should have a new file being added here to your system
where our data was written to and this is how easy you can write a file with Node.js.
Of course you can also read in a file, so parse a file, you do this with the filesystem here with read
file and then the path to the file, for example with user-data.txt,
then again you can set some arguments or just provide your callback here,
this is a callback function that takes two arguments, first argument is a potential error object which
will be undefined if we have no error,
second argument is the data we hopefully get and then here we can check if we do have an error and if
we do have one, we can console log the error and then maybe return to not continue with the execution
and otherwise, we'll get our data.
So let's see what we get there if we print this
and now if we save that and we run this script again, we see wrote to file but we also see this buffer
output here which is coming from parsing the file.
Now of course that buffer is not that useful, if we call to string on that, it gets more useful though,
now this is converted to a string and we got the content of the file being output there.
So this is a very simple Node.js application of course if you want to call it like this but we see
some Node.js APIs in action here and at the same time we see regular Javascript syntax, like the
if statement which we use here or the const keyword which you of course learned about throughout this
course.