1
00:00:02,000 --> 00:00:05,000
So now that we added our API routes

2
00:00:05,000 --> 00:00:07,000
or the newsletter in for the comments

3
00:00:07,000 --> 00:00:09,000
and that we wired up

4
00:00:09,000 --> 00:00:11,000
those front-end components for the newsletter

5
00:00:11,000 --> 00:00:13,000
and for the comments.

6
00:00:13,000 --> 00:00:16,000
Now that all of that was done.

7
00:00:16,000 --> 00:00:20,000
It's time to work on the logic inside of the API routes

8
00:00:20,000 --> 00:00:24,000
because at the moment we don't do anything in there.

9
00:00:24,000 --> 00:00:26,000
In the newsletter API route we're

10
00:00:26,000 --> 00:00:28,000
just logging the entered email

11
00:00:28,000 --> 00:00:30,000
and in the comments API route

12
00:00:30,000 --> 00:00:32,000
for the post case,

13
00:00:32,000 --> 00:00:34,000
we also in the end,

14
00:00:34,000 --> 00:00:37,000
just log our comment here.

15
00:00:37,000 --> 00:00:41,000
And that of course is not too helpful.

16
00:00:41,000 --> 00:00:42,000
That's not the goal.

17
00:00:42,000 --> 00:00:46,000
Instead, we wanna store that data more permanently

18
00:00:46,000 --> 00:00:49,000
and we could of course use a file for that.

19
00:00:49,000 --> 00:00:52,000
We could re-add a data folder

20
00:00:52,000 --> 00:00:55,000
in our project with some JSON

21
00:00:55,000 --> 00:00:59,000
or a text file and write that data to that file.

22
00:00:59,000 --> 00:01:03,000
But I would argue it's more realistic to use a database

23
00:01:03,000 --> 00:01:04,000
and therefore that's what we'll do.

24
00:01:04,000 --> 00:01:06,000
I'll use a real database here.

25
00:01:06,000 --> 00:01:08,000
I'll use MongoDB.

26
00:01:08,000 --> 00:01:11,000
MongoDB of course is just one option.

27
00:01:11,000 --> 00:01:13,000
It's not a must use option,

28
00:01:13,000 --> 00:01:17,000
but I will use it because using MongoDB

29
00:01:17,000 --> 00:01:21,000
with Node.js is particularly easy

30
00:01:21,000 --> 00:01:23,000
and it's also a production ready,

31
00:01:23,000 --> 00:01:27,000
scalable database which is a great choice

32
00:01:27,000 --> 00:01:29,000
for pretty much all projects.

33
00:01:29,000 --> 00:01:32,000
For a lot of projects, Let's put it like this.

34
00:01:32,000 --> 00:01:34,000
And that's why we'll use MongoDB.

35
00:01:34,000 --> 00:01:38,000
Even better than that, we can get started for free.

36
00:01:38,000 --> 00:01:41,000
If you visit MongoDB.com and you go to pricing,

37
00:01:41,000 --> 00:01:43,000
you'll see that they have various offerings

38
00:01:43,000 --> 00:01:46,000
and we will use the cloud service here,

39
00:01:46,000 --> 00:01:47,000
MongoDB Atlas.

40
00:01:49,000 --> 00:01:53,000
That is their fully managed cloud database service,

41
00:01:53,000 --> 00:01:55,000
which we can use without setting up anything

42
00:01:55,000 --> 00:01:57,000
on our local system, which is great.

43
00:01:57,000 --> 00:02:00,000
And we can get started for free here,

44
00:02:00,000 --> 00:02:01,000
which is awesome.

45
00:02:01,000 --> 00:02:04,000
So we can click on start free here actually.

46
00:02:06,000 --> 00:02:07,000
Or in the top right corner,

47
00:02:07,000 --> 00:02:09,000
click on Try Free,

48
00:02:09,000 --> 00:02:10,000
click on Cloud

49
00:02:10,000 --> 00:02:14,000
and then get started for free here by signing up.

50
00:02:14,000 --> 00:02:18,000
just fill out these basic details and sign up.

51
00:02:18,000 --> 00:02:20,000
I already do have an account

52
00:02:20,000 --> 00:02:22,000
so I will log in with that.

53
00:02:22,000 --> 00:02:25,000
And once you did sign up and log in,

54
00:02:25,000 --> 00:02:27,000
you should be greeted with some screen

55
00:02:27,000 --> 00:02:29,000
that looks something like this.

56
00:02:29,000 --> 00:02:32,000
You should be able to build a cluster

57
00:02:33,000 --> 00:02:36,000
and then there use a shared cluster.

58
00:02:36,000 --> 00:02:37,000
So, create a cluster here

59
00:02:37,000 --> 00:02:40,000
where you can get started for free.

60
00:02:40,000 --> 00:02:44,000
No, a cluster is basically a server.

61
00:02:44,000 --> 00:02:45,000
You could say,

62
00:02:45,000 --> 00:02:48,000
which will then later host your databases,

63
00:02:48,000 --> 00:02:49,000
you could say.

64
00:02:49,000 --> 00:02:52,000
Now here we need to choose a cloud provider

65
00:02:52,000 --> 00:02:53,000
which will be used under the hood.

66
00:02:53,000 --> 00:02:55,000
And we can stick to AWS here

67
00:02:55,000 --> 00:02:58,000
and basically keep the defaults

68
00:02:59,000 --> 00:03:01,000
for a cluster tier let's stick

69
00:03:01,000 --> 00:03:06,000
to the M0 Sandbox since that's available for free.

70
00:03:07,000 --> 00:03:09,000
And for the additional settings,

71
00:03:09,000 --> 00:03:11,000
we also can stick to the defaults

72
00:03:11,000 --> 00:03:13,000
and we can also keep that default cluster name

73
00:03:13,000 --> 00:03:16,000
and create that cluster.

74
00:03:16,000 --> 00:03:19,000
Now, this will set up this database cluster

75
00:03:19,000 --> 00:03:20,000
which will then enable us to connect

76
00:03:20,000 --> 00:03:23,000
to this cluster and store data

77
00:03:23,000 --> 00:03:25,000
in the databases there.

78
00:03:25,000 --> 00:03:28,000
And using it will be super simple.

79
00:03:28,000 --> 00:03:29,000
Now, whilst this is getting set up,

80
00:03:29,000 --> 00:03:32,000
we can go back to our project already

81
00:03:32,000 --> 00:03:34,000
and quit the dev server

82
00:03:34,000 --> 00:03:36,000
and install an extra package

83
00:03:36,000 --> 00:03:37,000
with npm install.

84
00:03:38,000 --> 00:03:40,000
Because in order to communicate

85
00:03:40,000 --> 00:03:45,000
with MongoDB and to send queries to MongoDB,

86
00:03:45,000 --> 00:03:49,000
we need to install a new third party package.

87
00:03:49,000 --> 00:03:51,000
If you search for Node.js MongoDB,

88
00:03:51,000 --> 00:03:53,000
you also find it on npm.

89
00:03:53,000 --> 00:03:55,000
It's this MongoDB package

90
00:03:55,000 --> 00:03:59,000
which is the official driver for MongoDB.

91
00:03:59,000 --> 00:04:01,000
So that makes connecting and talking

92
00:04:01,000 --> 00:04:05,000
to the Mongo DB database super easy then.

93
00:04:05,000 --> 00:04:08,000
So we should install MongoDB,

94
00:04:08,000 --> 00:04:10,000
this MongoDB package here like this

95
00:04:10,000 --> 00:04:13,000
with npm installed MongoDB.

96
00:04:13,000 --> 00:04:15,000
And this will install this third party package

97
00:04:15,000 --> 00:04:17,000
which we'll then use in our server side code

98
00:04:17,000 --> 00:04:20,000
to talk to MongoDB to our cluster

99
00:04:20,000 --> 00:04:22,000
and to store data and

100
00:04:22,000 --> 00:04:24,000
to fetch data as well Of course.

101
00:04:24,000 --> 00:04:27,000
And the important thing here really is

102
00:04:27,000 --> 00:04:30,000
that we'll use it in our server side code.

103
00:04:30,000 --> 00:04:32,000
So in these API routes,

104
00:04:32,000 --> 00:04:36,000
we will not use this Mongo DB package

105
00:04:36,000 --> 00:04:38,000
in any client side code.

106
00:04:38,000 --> 00:04:41,000
A, because it's not optimized for that.

107
00:04:41,000 --> 00:04:43,000
It might not even work there because there

108
00:04:43,000 --> 00:04:48,000
is a high chance that it uses Node.js APIs internally

109
00:04:48,000 --> 00:04:51,000
and b, even if it does work in the browser,

110
00:04:51,000 --> 00:04:54,000
it would be a security issue.

111
00:04:54,000 --> 00:04:56,000
Because for talking to the database,

112
00:04:56,000 --> 00:04:59,000
we will need to plug our credentials

113
00:04:59,000 --> 00:05:01,000
into the code and we don't wanna do

114
00:05:01,000 --> 00:05:04,000
that in any code our users can see

115
00:05:04,000 --> 00:05:07,000
because they could then abuse those credentials

116
00:05:07,000 --> 00:05:09,000
to hack into our database.

117
00:05:09,000 --> 00:05:14,000
That's why we want to run such database code only

118
00:05:14,000 --> 00:05:16,000
on the server side.

119
00:05:16,000 --> 00:05:18,000
So, only in our API routes.

120
00:05:20,000 --> 00:05:23,000
Now connecting to the cluster is done here now.

121
00:05:23,000 --> 00:05:25,000
And when we clicked on Connect,

122
00:05:25,000 --> 00:05:28,000
we can choose connect your application.

123
00:05:28,000 --> 00:05:32,000
And we see code which we can use to connect

124
00:05:32,000 --> 00:05:37,000
to our MongoDB cluster with the Node.js driver here.

125
00:05:37,000 --> 00:05:38,000
And that's what we're going

126
00:05:38,000 --> 00:05:40,000
to do in the next lectures.

