1
00:00:04,160 --> 00:00:05,600
Welcome back.

2
00:00:05,630 --> 00:00:08,390
Let's get started with the final project.

3
00:00:08,390 --> 00:00:11,840
That is the API creation for this section.

4
00:00:12,770 --> 00:00:21,240
We are going to write all the content or data onto a file using the FS module from Node.js.

5
00:00:21,500 --> 00:00:23,270
Well, so let's get started.

6
00:00:23,510 --> 00:00:30,500
The first thing is we are going to create a utility function to create folder and files dynamically.

7
00:00:30,980 --> 00:00:37,600
Again, we can do it manually by right clicking and click on name folder and the name of the folder.

8
00:00:37,610 --> 00:00:43,340
And after that I can go ahead and create a file called post dot JSON file.

9
00:00:43,370 --> 00:00:50,390
But let's make use of our knowledge in Node.js to create the folders and files dynamically.

10
00:00:50,630 --> 00:00:58,970
So at the root of my projects, let's create a file called Utils dot js and you are going to create

11
00:00:58,970 --> 00:01:03,830
two functions to create a folder and a file dynamically.

12
00:01:04,129 --> 00:01:07,340
So we need the FS module to get it done.

13
00:01:07,340 --> 00:01:13,100
So let's require the FS module and here we go.

14
00:01:13,670 --> 00:01:18,260
So let's give a beautiful comment here says create folder.

15
00:01:18,260 --> 00:01:20,300
And here is a function.

16
00:01:20,300 --> 00:01:26,660
I'm going to use every function in this time around we can make use of normal function call and for

17
00:01:26,660 --> 00:01:30,860
this function we pass a the folder that we want to create.

18
00:01:30,860 --> 00:01:33,560
So I'll call this one as folder name.

19
00:01:34,460 --> 00:01:39,360
So inside here we are going to write the code in synchronous way.

20
00:01:39,380 --> 00:01:40,160
Why?

21
00:01:40,190 --> 00:01:45,770
Simply because we want to create the folder before even the server gets started.

22
00:01:45,770 --> 00:01:50,120
Because the application depends on this folder and the file.

23
00:01:50,120 --> 00:01:52,560
So we are going to make user for a synchronous way.

24
00:01:52,580 --> 00:01:57,360
So inside here first let's check if the folder exists.

25
00:01:57,380 --> 00:02:00,890
So here check if folder.

26
00:02:02,260 --> 00:02:03,090
Exist.

27
00:02:03,490 --> 00:02:05,640
So here we go, Eve.

28
00:02:05,650 --> 00:02:14,770
And then f s we have a method called exist sync synchronize, then pass in the folder name.

29
00:02:15,040 --> 00:02:17,490
So here, if it doesn't exist.

30
00:02:17,500 --> 00:02:19,120
So let's inject it.

31
00:02:19,390 --> 00:02:24,450
So if it doesn't exist, then we can go ahead and create the folder too.

32
00:02:24,490 --> 00:02:25,180
Here you go.

33
00:02:25,180 --> 00:02:34,210
Create the folder and you are going to use the synchronous way making use of k d r sync and then the

34
00:02:34,210 --> 00:02:37,480
name of the folder are past in the folder name.

35
00:02:37,660 --> 00:02:40,250
Now I'm done with the folder creation.

36
00:02:40,270 --> 00:02:43,120
What about creating a file function?

37
00:02:43,240 --> 00:02:45,760
So here are going to be create file.

38
00:02:45,790 --> 00:02:47,050
And here we go.

39
00:02:47,080 --> 00:02:48,190
Const.

40
00:02:49,120 --> 00:02:56,200
Kong creates file every function as that for file creation.

41
00:02:56,200 --> 00:03:01,900
First you need to pass in the file we want to create and then the content that you want to write onto

42
00:03:01,900 --> 00:03:07,810
the file because the FS module requires us to pass in these two arguments.

43
00:03:07,810 --> 00:03:16,420
So now inside the function body here again, let's check if file exists as we did in the folder creation

44
00:03:16,420 --> 00:03:17,110
function.

45
00:03:17,440 --> 00:03:29,560
So the same process begins of Eve, then f s dot exit sync and in here we pass in the path of the file

46
00:03:29,590 --> 00:03:30,970
that we want to check.

47
00:03:31,510 --> 00:03:38,440
And then if it doesn't exist, then we can go ahead and then write the content onto the file.

48
00:03:38,530 --> 00:03:40,810
So here let's inject it.

49
00:03:40,900 --> 00:03:43,900
So now inside the function body here.

50
00:03:45,450 --> 00:03:46,980
Remixes of F.

51
00:03:46,980 --> 00:03:55,770
S Dart, right file, sync and pass in the file you want to create, and then the content you want to

52
00:03:55,770 --> 00:03:57,120
write onto the file.

53
00:03:57,450 --> 00:04:00,810
So let's go ahead and then export these two functions.

54
00:04:00,810 --> 00:04:06,030
So here export equal to object because we have multiple functions here.

55
00:04:06,090 --> 00:04:10,530
The first one going to be create folder and then create file.

56
00:04:10,860 --> 00:04:20,070
So here let's require those functions so equal to required and then it's inside the utils file.

57
00:04:20,519 --> 00:04:28,710
So here let's do structure and get those two files, create file and then create folder, but make sure

58
00:04:28,710 --> 00:04:30,630
that your server is not running.

59
00:04:30,630 --> 00:04:33,090
So let me clear the console here and shut down.

60
00:04:33,120 --> 00:04:38,880
I'll show you why because I want to see it being creating when we started manually.

61
00:04:38,880 --> 00:04:39,510
All right.

62
00:04:39,630 --> 00:04:44,010
So now let's come here and say that create.

63
00:04:46,950 --> 00:04:47,990
And here you go.

64
00:04:48,000 --> 00:04:49,740
Going to be great fodder.

65
00:04:49,740 --> 00:04:52,680
And I want to create a folder called Data.

66
00:04:53,130 --> 00:04:54,990
If we see that here, there is no folder.

67
00:04:54,990 --> 00:04:59,420
But as soon as I restart a server here, now check it out.

68
00:04:59,430 --> 00:05:02,280
You see that we have the folder being created.

69
00:05:02,400 --> 00:05:03,690
So it is function.

70
00:05:03,690 --> 00:05:05,370
I can create different folders.

71
00:05:05,370 --> 00:05:07,410
Let's say public folder here.

72
00:05:07,440 --> 00:05:11,610
Save it now it has created I can create subfolder.

73
00:05:11,610 --> 00:05:16,920
So now inside a public folder I can create a folder called as I see save it.

74
00:05:16,920 --> 00:05:23,430
And now you see I have a C folder I can also create, let's say CSS folder into it.

75
00:05:23,580 --> 00:05:29,160
And now we see that I have these two folders, but I want the data for the OLLI.

76
00:05:29,160 --> 00:05:32,520
So let me move this one and then let me provide data.

77
00:05:32,940 --> 00:05:34,950
Now I'm done with the folder creation.

78
00:05:34,960 --> 00:05:37,770
The next step is I need to create the file.

79
00:05:37,770 --> 00:05:42,210
So let me move this forward because I don't need it in this particular project.

80
00:05:42,510 --> 00:05:50,010
Next is create file, function, create file and I'll make use of create file.

81
00:05:50,010 --> 00:05:52,320
And this function takes in two things.

82
00:05:52,440 --> 00:05:54,840
Look at IntelliSense, the file and the content.

83
00:05:54,840 --> 00:05:59,220
So here I want to create a file inside the data folder here.

84
00:05:59,220 --> 00:06:06,090
So I provide data for a slash and then the name of the file is going to be post JSON file.

85
00:06:06,330 --> 00:06:08,610
The next argument is going to be the contents.

86
00:06:08,610 --> 00:06:13,710
Let me say content here for the entire colon contents here.

87
00:06:14,280 --> 00:06:14,700
All right.

88
00:06:14,700 --> 00:06:17,760
So let's save it and let's see, I've saved it.

89
00:06:17,760 --> 00:06:19,410
The seven password restarted.

90
00:06:19,410 --> 00:06:21,390
So let's see if there will be an error.

91
00:06:21,390 --> 00:06:22,290
No error.

92
00:06:22,290 --> 00:06:31,920
So let's open the folder and I have the file and we have the contents on that bad guys because we are

93
00:06:31,920 --> 00:06:40,170
going to deal with post with an array and must be in adjacent data and upon server restarting of the

94
00:06:40,170 --> 00:06:41,150
application.

95
00:06:41,430 --> 00:06:46,740
These functions will not be called by the user, but instead we need to start it automatically.

96
00:06:46,740 --> 00:06:52,410
So instead of providing this text here, let's provide a sample array of posts.

97
00:06:52,410 --> 00:06:54,480
So back to the utils here.

98
00:06:54,480 --> 00:06:56,850
We are going to make changes to this function.

99
00:06:57,090 --> 00:06:59,760
Now let's provide a default post here.

100
00:06:59,760 --> 00:07:00,540
So here we go.

101
00:07:00,540 --> 00:07:02,250
Const defaults.

102
00:07:03,240 --> 00:07:04,320
Default.

103
00:07:05,280 --> 00:07:06,120
Post.

104
00:07:07,600 --> 00:07:12,460
Is equal to a ray and then going to be a JSON format.

105
00:07:12,580 --> 00:07:13,630
So here we go.

106
00:07:13,630 --> 00:07:16,780
First we provide the ID of the post.

107
00:07:16,780 --> 00:07:25,600
For the meantime, let's give it as 2020 as the ID and come on because it's JSON, the property and

108
00:07:25,600 --> 00:07:27,700
the value must be in code.

109
00:07:28,000 --> 00:07:35,650
The next property is called a title and now let's give it a title, let's say a HTML then comma.

110
00:07:35,680 --> 00:07:44,470
The next property is the u r of the post and let's provide a http colon voice for slash.

111
00:07:44,470 --> 00:07:45,700
Let's see some.

112
00:07:45,700 --> 00:07:47,350
You are a dotcom.

113
00:07:47,830 --> 00:07:56,140
The next property is going to be the description must be in double coat description of the post and

114
00:07:56,140 --> 00:08:00,040
let's say description going to be the best.

115
00:08:00,520 --> 00:08:00,940
All right.

116
00:08:00,940 --> 00:08:04,330
If I save it, it's going to change everything into objects.

117
00:08:04,330 --> 00:08:05,500
So let's save it and see.

118
00:08:06,610 --> 00:08:10,900
When I save it, it's going to change the JSON format into objects.

119
00:08:10,900 --> 00:08:14,980
So let's save it and we see that as we move the code around it.

120
00:08:15,190 --> 00:08:20,590
So we need to wrap everything into a code so that we see it as a string.

121
00:08:20,620 --> 00:08:21,580
Let me undo.

122
00:08:21,580 --> 00:08:24,790
And now let's wrap everything in code or double code.

123
00:08:24,790 --> 00:08:26,050
So here we go.

124
00:08:26,080 --> 00:08:29,380
If I save it, I still see the JSON format.

125
00:08:29,380 --> 00:08:34,480
So now for this function, we don't need to pass in the content upon column because I'm going to pass

126
00:08:34,480 --> 00:08:37,539
it here to the function call here.

127
00:08:37,690 --> 00:08:39,460
Now, here we go.

128
00:08:39,850 --> 00:08:44,590
So if I say we have saved a file, now let's see.

129
00:08:44,830 --> 00:08:50,440
You could see that I don't see the post here because the file exists, so it's going to create again.

130
00:08:50,440 --> 00:08:52,300
So let's remove the file.

131
00:08:52,390 --> 00:09:00,700
And now when I save it, save it and going to create the post here when I open it, I have the post

132
00:09:00,700 --> 00:09:01,390
here.

133
00:09:01,660 --> 00:09:06,970
So with this one I can go ahead and make a request to fetch this post.

134
00:09:07,000 --> 00:09:07,870
That is it.

135
00:09:07,870 --> 00:09:09,520
That is crazy, guys.

136
00:09:09,520 --> 00:09:16,320
So this is how we're able to write a function to create a file and folders dynamically.

137
00:09:16,330 --> 00:09:23,410
Then this video, let's go ahead and implement a route to fetch this post and display it inside.

138
00:09:23,410 --> 00:09:24,010
Postman.

