1
00:00:02,120 --> 00:00:05,540
So in this basic script here, just output something in the console.

2
00:00:05,540 --> 00:00:11,000
Now how about writing this data to a file and then reading the data in from a file?

3
00:00:11,150 --> 00:00:18,050
For that, we need a couple of packages which are available when you install Node.js. Node.js works such

4
00:00:18,050 --> 00:00:23,450
that certain, very important features are available globally, for example setTimeout.

5
00:00:23,450 --> 00:00:28,880
This is a browser API that was also implemented in Node.js and you can just use it like this in

6
00:00:28,880 --> 00:00:35,990
any Node.js file but other functionalities which you don't need that often need to be imported explicitly,

7
00:00:36,140 --> 00:00:42,230
for example functionalities for working with the filesystem, for that Node.js works with so-called

8
00:00:42,320 --> 00:00:48,440
modules and on nodejs.org, you can learn more about that. In the docs there,

9
00:00:48,500 --> 00:00:54,590
if you click on API reference, you'll find all the packages, all the core modules Node.js is made up

10
00:00:54,590 --> 00:00:55,240
of and

11
00:00:55,340 --> 00:01:00,860
for example here under f you find the filesystem module and you find the complete documentation for this

12
00:01:00,860 --> 00:01:01,400
module.

13
00:01:01,400 --> 00:01:06,110
Now as you can tell, there's a lot in there and of course we'll just briefly dive into this but we will

14
00:01:06,110 --> 00:01:07,590
use it a bit at least.

15
00:01:07,880 --> 00:01:14,690
So there for example we see how we can make this module and its features available in a Node.js script

16
00:01:14,930 --> 00:01:18,020
and that's one important difference to browser side scripts,

17
00:01:18,080 --> 00:01:21,860
the Node.js import export syntax looks a bit different,

18
00:01:21,890 --> 00:01:29,060
instead of using the import and export keywords, in Node.js you use require to require a module, be

19
00:01:29,060 --> 00:01:31,190
that a module provided by Node.js

20
00:01:31,190 --> 00:01:37,760
like in this example or be that your own file and then you store the required data in your own constant

21
00:01:37,970 --> 00:01:43,850
which you can then use in your script and I will later also show you how you export something.

22
00:01:43,850 --> 00:01:50,660
So with that, let's copy that and add this here in our app.js file and this pulls in the filesystem module

23
00:01:50,720 --> 00:01:56,060
which again was already installed as part of Node.js, so we don't need to install it separately but

24
00:01:56,060 --> 00:02:03,320
we still need to add it to this file with this line and now we can use the filesystem object here to

25
00:02:03,320 --> 00:02:12,880
work with the filesystem, for example to call the write file method here which takes a path to a file

26
00:02:12,910 --> 00:02:18,910
and then the data we want to write to that file and there we could, as a simple starting point, just provide

27
00:02:18,910 --> 00:02:23,340
a file name and then this will be written in the same path as the script sits in,

28
00:02:23,350 --> 00:02:30,730
for example we could write this to user-data.txt let's say, to write a text file and then

29
00:02:30,790 --> 00:02:33,080
as a data, we could have some text here,

30
00:02:33,130 --> 00:02:41,190
user name equals Max for example. You can also defined some write options here as a third argument where

31
00:02:41,190 --> 00:02:45,300
you can set things like the encoding and so on but we don't need this here, we can just execute this

32
00:02:45,300 --> 00:02:46,470
file like this.

33
00:02:46,470 --> 00:02:52,140
Now write file then takes a callback as a third argument which potentially holds an error if it failed

34
00:02:52,320 --> 00:02:57,750
and we can then check if we do have an error, in which case we could console log it for example and otherwise

35
00:02:57,750 --> 00:03:05,910
we know we succeeded and we could console log wrote to file and now with that if we now execute our app.js

36
00:03:05,910 --> 00:03:12,210
file, you see wrote to file here and you should have a new file being added here to your system

37
00:03:12,420 --> 00:03:17,800
where our data was written to and this is how easy you can write a file with Node.js.

38
00:03:18,060 --> 00:03:24,510
Of course you can also read in a file, so parse a file, you do this with the filesystem here with read

39
00:03:24,570 --> 00:03:29,950
file and then the path to the file, for example with user-data.txt,

40
00:03:30,120 --> 00:03:35,450
then again you can set some arguments or just provide your callback here,

41
00:03:35,490 --> 00:03:40,560
this is a callback function that takes two arguments, first argument is a potential error object which

42
00:03:40,560 --> 00:03:42,780
will be undefined if we have no error,

43
00:03:42,780 --> 00:03:50,040
second argument is the data we hopefully get and then here we can check if we do have an error and if

44
00:03:50,040 --> 00:03:55,800
we do have one, we can console log the error and then maybe return to not continue with the execution

45
00:03:56,190 --> 00:03:58,240
and otherwise, we'll get our data.

46
00:03:58,620 --> 00:04:01,180
So let's see what we get there if we print this

47
00:04:01,420 --> 00:04:06,570
and now if we save that and we run this script again, we see wrote to file but we also see this buffer

48
00:04:06,570 --> 00:04:08,980
output here which is coming from parsing the file.

49
00:04:09,020 --> 00:04:15,750
Now of course that buffer is not that useful, if we call to string on that, it gets more useful though,

50
00:04:15,750 --> 00:04:20,450
now this is converted to a string and we got the content of the file being output there.

51
00:04:20,490 --> 00:04:25,830
So this is a very simple Node.js application of course if you want to call it like this but we see

52
00:04:25,830 --> 00:04:31,560
some Node.js APIs in action here and at the same time we see regular Javascript syntax, like the

53
00:04:31,560 --> 00:04:36,750
if statement which we use here or the const keyword which you of course learned about throughout this

54
00:04:36,750 --> 00:04:37,200
course.
