1
00:00:00,900 --> 00:00:05,980
Let's get to work on the post method for our cells router, so to get started, we're going to first

2
00:00:06,000 --> 00:00:08,460
import in the first module at the top of this file.

3
00:00:09,180 --> 00:00:13,370
So at the very top, I'm going to import FHS from promises.

4
00:00:13,980 --> 00:00:19,320
And remember, the first module that is included in the node standard library is all about saving files

5
00:00:19,320 --> 00:00:20,880
or loading files on the hard drive.

6
00:00:21,450 --> 00:00:25,020
The default module all operates using callback functions.

7
00:00:25,050 --> 00:00:26,790
So there's a lot of functions inside there.

8
00:00:26,970 --> 00:00:31,740
They all involve some amount of asynchronous code and that a synchronicity is handled using callback

9
00:00:31,740 --> 00:00:32,280
functions.

10
00:00:32,490 --> 00:00:37,680
In general, you and I do not really like callback functions, so we are using a sub module called Promises.

11
00:00:38,250 --> 00:00:43,950
This is the exact same kind of module, but all these different functions inside of your return promises

12
00:00:43,980 --> 00:00:48,870
which will allow us to write out async await code rather than relying upon Nasti callbacks.

13
00:00:49,680 --> 00:00:52,880
OK, so let's then go back down to our post handler right here.

14
00:00:53,220 --> 00:00:57,600
Now, when we were discussing just a moment ago the fact we might need to make sure a file exists and

15
00:00:57,600 --> 00:01:00,690
if it didn't, to create it, that's actually not technically created.

16
00:01:00,780 --> 00:01:02,790
And I probably shouldn't have put that comment inside there.

17
00:01:02,840 --> 00:01:03,590
That was my mistake.

18
00:01:04,709 --> 00:01:09,390
Whenever we write a file to the hard drive using this function inside the module that you and I are

19
00:01:09,390 --> 00:01:13,170
going to use, if that file doesn't exist, it will be created for us automatically.

20
00:01:13,440 --> 00:01:14,830
So I'm just going to take that comment out.

21
00:01:14,850 --> 00:01:16,110
It's something we don't really care about.

22
00:01:17,710 --> 00:01:21,760
So then next up, we're going to take care of the first coming right here, so we want to reach into

23
00:01:21,760 --> 00:01:22,690
our request object.

24
00:01:22,900 --> 00:01:27,400
We're going to assume that there is a list of cells inside there and we need to somehow take that list

25
00:01:27,400 --> 00:01:31,450
of cells and then turn them into some format that can be safely written into that file.

26
00:01:33,210 --> 00:01:39,810
So for that, let's assume that there is maybe a cels property on record body, so I'm just absolutely

27
00:01:39,810 --> 00:01:41,770
assuming that we are going to have a cels property.

28
00:01:41,940 --> 00:01:46,740
Remember, we currently do not have any code inside of a reactor application related to making requests

29
00:01:46,740 --> 00:01:47,670
off to this API.

30
00:01:48,030 --> 00:01:51,750
So we're kind of just setting ourselves up right here, just something we need to remember when we eventually

31
00:01:51,750 --> 00:01:54,720
go back over to our react up and start to send the list of cells off.

32
00:01:56,270 --> 00:02:02,030
And a list of cells is going to be an array of objects for each object is going to have an ID, a content

33
00:02:02,030 --> 00:02:04,310
property and remember a type as well.

34
00:02:04,430 --> 00:02:06,800
And type will be either text or code.

35
00:02:08,310 --> 00:02:13,080
We might want to apply some kind of type information to cells right here just to help TypeScript understand

36
00:02:13,080 --> 00:02:14,270
what we are really working with.

37
00:02:14,730 --> 00:02:21,600
So at the top of this file, I'm going to add in an interface of type cell, and I'm going to say that

38
00:02:21,600 --> 00:02:24,900
this is going to be something that has an ID, that is a string.

39
00:02:26,550 --> 00:02:28,020
Content, that is a string.

40
00:02:29,910 --> 00:02:33,600
And type, that will be either text or code.

41
00:02:36,600 --> 00:02:42,060
Then back down inside of our post method, I'm going to annotate the type of cells with a colon right

42
00:02:42,060 --> 00:02:45,570
there, cells is a array of cells.

43
00:02:46,720 --> 00:02:50,410
Now, if we mouseover cells, we should be told, yep, it is an array of cells.

44
00:02:53,380 --> 00:02:58,540
Now, that is an array of objects, so now we can probably just take that array of objects, maybe turn

45
00:02:58,540 --> 00:03:02,710
it into a string using JSON String Afie and then write that into the file.

46
00:03:03,310 --> 00:03:07,720
And remember, the file we are talking about right now is coming from that file name in that directory

47
00:03:07,720 --> 00:03:08,140
right there.

48
00:03:10,090 --> 00:03:16,690
So to actually write this data into a file, we would do a wait for DOT.

49
00:03:18,080 --> 00:03:21,270
Right file we then put in the path to the file.

50
00:03:21,290 --> 00:03:25,490
We want to write to so in this case, it's going to be a combination of the file name and the directory.

51
00:03:26,390 --> 00:03:30,650
Just to make sure that we form up this entire path correctly, I'm going to import the path module at

52
00:03:30,650 --> 00:03:31,250
the very top.

53
00:03:35,690 --> 00:03:40,700
And then right above all of our router stuff or the router market in the post, I'm going to add in.

54
00:03:41,670 --> 00:03:46,530
Full path right here, and that will be join directory and file name.

55
00:03:51,010 --> 00:03:54,340
So then back down here, we'll do a write file to Full Path.

56
00:03:55,400 --> 00:04:00,170
We want to write into that JSON string of ifI cells.

57
00:04:01,150 --> 00:04:05,830
And we're going to put on a third argument here to just describe the type of information or how we want

58
00:04:05,830 --> 00:04:07,810
this data written into the file.

59
00:04:08,050 --> 00:04:11,980
I want to encoded as UTF eight, which pretty much just means plain text.

60
00:04:13,500 --> 00:04:17,769
So after that, we can go ahead and send back a response and say, hey, everything went successfully,

61
00:04:18,350 --> 00:04:20,420
we're not going to add in any air handling just yet.

62
00:04:20,420 --> 00:04:27,410
But of course, we might want to eventually we'll do a resort send with maybe a status, OK, or something

63
00:04:27,410 --> 00:04:28,160
similar to that.

64
00:04:29,940 --> 00:04:31,800
OK, so pretty easy to get started.

65
00:04:32,750 --> 00:04:33,810
Let's say this right here.

66
00:04:33,920 --> 00:04:38,660
Come back in just a moment, we'll start to take care of the get to slash cells and then eventually

67
00:04:38,660 --> 00:04:42,830
we'll go back over to a react application and make some requests to these two different root handlers.

