1
00:00:02,040 --> 00:00:02,943
Now, let's evolve

2
00:00:02,943 --> 00:00:06,283
this very simple first API a little bit.

3
00:00:07,360 --> 00:00:11,070
For this, I'll first of all add a different folder structure

4
00:00:11,070 --> 00:00:13,630
even though for this simple API,

5
00:00:13,630 --> 00:00:15,860
we, of course, wouldn't need that.

6
00:00:15,860 --> 00:00:18,050
But I wanna show that you still build it

7
00:00:18,050 --> 00:00:20,470
as all the other websites you learned about

8
00:00:20,470 --> 00:00:22,230
throughout this course

9
00:00:22,230 --> 00:00:25,360
just with different data that's being sent back

10
00:00:25,360 --> 00:00:26,563
with the response.

11
00:00:27,400 --> 00:00:29,450
Hence I'll add a controllers folder

12
00:00:29,450 --> 00:00:32,000
and a models folder.

13
00:00:32,000 --> 00:00:33,580
And I'll add a data folder

14
00:00:33,580 --> 00:00:35,740
with this database.js file,

15
00:00:35,740 --> 00:00:39,143
which I will soon use for connecting to the database.

16
00:00:40,140 --> 00:00:43,250
And I will also add a routes folder.

17
00:00:43,250 --> 00:00:44,420
In that routes folder,

18
00:00:44,420 --> 00:00:48,070
I'll add a quotes.routes.js file.

19
00:00:48,070 --> 00:00:49,560
In the controllers folder,

20
00:00:49,560 --> 00:00:53,960
I'll add a quotes.controller.js file.

21
00:00:53,960 --> 00:00:55,710
And in the models folder,

22
00:00:55,710 --> 00:00:59,670
I'll add a quote.model.js file.

23
00:00:59,670 --> 00:01:01,700
Again, this is all overkill

24
00:01:01,700 --> 00:01:03,743
but it makes for a great demo.

25
00:01:05,040 --> 00:01:07,490
Now, let's start in the routes file.

26
00:01:07,490 --> 00:01:11,460
There, as before, I, of course, wanna import express

27
00:01:11,460 --> 00:01:16,460
by requiring express here like this.

28
00:01:16,460 --> 00:01:18,550
And then get access to the router

29
00:01:18,550 --> 00:01:21,420
by calling express.Router

30
00:01:21,420 --> 00:01:24,283
as we did it many times before in this course.

31
00:01:25,160 --> 00:01:28,750
And of course, we also wanna export this router in the end

32
00:01:28,750 --> 00:01:31,090
but in between, we wanna configure it

33
00:01:31,090 --> 00:01:34,280
and, for example, add our get route here.

34
00:01:34,280 --> 00:01:37,490
This one get route for /quote

35
00:01:37,490 --> 00:01:41,660
or if we load these routes such that /quote

36
00:01:41,660 --> 00:01:45,410
is already set as a prefix and filter in app.js,

37
00:01:45,410 --> 00:01:48,000
we can have just slash here,

38
00:01:48,000 --> 00:01:50,083
and then point at our controller.

39
00:01:51,170 --> 00:01:54,000
And then to show you what I mean with that prefix,

40
00:01:54,000 --> 00:01:58,190
in app.js, we can, of course, include our routes.

41
00:01:58,190 --> 00:02:01,190
So here I can add my quoteRoutes

42
00:02:01,190 --> 00:02:06,190
by requiring ./routes/quote.routes like this.

43
00:02:07,830 --> 00:02:11,780
And then instead of registering this route here like that,

44
00:02:11,780 --> 00:02:14,000
we can use app.use

45
00:02:14,000 --> 00:02:17,360
and then add /routes as a prefix here,

46
00:02:17,360 --> 00:02:19,763
and as you learned, this will act as a filter.

47
00:02:20,750 --> 00:02:23,830
And it will then be removed from the path thereafter

48
00:02:23,830 --> 00:02:27,910
and then matching requests with that prefix removed

49
00:02:27,910 --> 00:02:30,540
from there path will be forwarded

50
00:02:30,540 --> 00:02:32,630
to the middleware we specify here.

51
00:02:32,630 --> 00:02:35,360
In this case, to our quoteRoutes config

52
00:02:35,360 --> 00:02:37,570
where the router will then check

53
00:02:37,570 --> 00:02:41,310
which route best fits this incoming request.

54
00:02:41,310 --> 00:02:43,530
And here, of course, we only have one route,

55
00:02:43,530 --> 00:02:46,743
so if that doesn't fit, no route will handle this.

56
00:02:48,170 --> 00:02:51,160
So that's this first step here.

57
00:02:51,160 --> 00:02:53,710
Now we can add the quotes.controller

58
00:02:53,710 --> 00:02:56,330
or add some functions to it to be precise.

59
00:02:56,330 --> 00:03:01,330
For example, the getRandomQuote function,

60
00:03:01,470 --> 00:03:04,220
which will be a middleware in the end,

61
00:03:04,220 --> 00:03:06,940
as all controller actions are.

62
00:03:06,940 --> 00:03:11,940
And hence here we get request, response and next as always.

63
00:03:13,210 --> 00:03:16,420
And here we then can send back our data,

64
00:03:16,420 --> 00:03:19,110
which I sent back before as well

65
00:03:19,110 --> 00:03:23,073
with res.json and quote set to our quote.

66
00:03:23,980 --> 00:03:27,110
We'll get a random quote here soon.

67
00:03:27,110 --> 00:03:29,370
And then, of course, we wanna expose that

68
00:03:29,370 --> 00:03:31,810
and here we just have one function,

69
00:03:31,810 --> 00:03:35,070
so we could directly export it like this

70
00:03:35,070 --> 00:03:38,550
but if we anticipate that we might have more functions,

71
00:03:38,550 --> 00:03:40,290
more actions in the future,

72
00:03:40,290 --> 00:03:43,180
we can instead export an object,

73
00:03:43,180 --> 00:03:45,470
add a key of getRandomQuote

74
00:03:45,470 --> 00:03:48,153
and point at the getRandomQuote function.

75
00:03:49,370 --> 00:03:53,140
And with that, we can then go to quotes.routes

76
00:03:53,140 --> 00:03:55,860
and include our controller here.

77
00:03:55,860 --> 00:03:58,610
And I'm going through that with a bit more speed

78
00:03:58,610 --> 00:04:01,630
because these are all things we did multiple times

79
00:04:01,630 --> 00:04:03,090
throughout the course already.

80
00:04:03,090 --> 00:04:05,420
There's nothing new about this.

81
00:04:05,420 --> 00:04:07,530
I include my controller here

82
00:04:07,530 --> 00:04:10,883
from controllers/quotes.controller like that.

83
00:04:11,970 --> 00:04:13,510
And then here for this route,

84
00:04:13,510 --> 00:04:16,783
we can point at quotesController.getRandomQuote.

85
00:04:19,220 --> 00:04:21,053
Now, this would be our first step.

86
00:04:22,420 --> 00:04:24,960
Now, I wanna store my quotes in a database

87
00:04:24,960 --> 00:04:27,530
and then get random quotes from there

88
00:04:27,530 --> 00:04:32,530
and therefore, I will install mongodb, this driver

89
00:04:32,870 --> 00:04:35,633
so that I can send requests to the database.

90
00:04:38,570 --> 00:04:39,403
Like this.

91
00:04:41,460 --> 00:04:45,570
And I will also install as a development dependency

92
00:04:45,570 --> 00:04:48,020
with save-dev nodemon

93
00:04:48,020 --> 00:04:52,030
so that we can use that for running our Node server.

94
00:04:52,030 --> 00:04:55,040
That's, of course, optional, as it always was.

95
00:04:55,040 --> 00:04:57,500
But with that in package.json,

96
00:04:57,500 --> 00:05:00,320
I'll quickly edit this script,

97
00:05:00,320 --> 00:05:01,660
and add a start script

98
00:05:01,660 --> 00:05:04,193
where I use nodemon to run app.js.

99
00:05:05,450 --> 00:05:08,440
And then more importantly, in the database.js file,

100
00:05:08,440 --> 00:05:12,120
I wanna set up a connection to my MongoDB database.

101
00:05:12,120 --> 00:05:15,890
And for this, I have a MongoDB database server running

102
00:05:15,890 --> 00:05:17,300
on my local machine.

103
00:05:17,300 --> 00:05:20,000
As in many other course sections,

104
00:05:20,000 --> 00:05:22,330
if you wanna follow along on your machine,

105
00:05:22,330 --> 00:05:24,530
you need to make sure you have one installed

106
00:05:24,530 --> 00:05:26,810
and up and running on your machine as well

107
00:05:26,810 --> 00:05:30,503
just as shown before in the course in the MongoDB sections.

108
00:05:32,250 --> 00:05:36,973
So in here we can then import mongodb like this.

109
00:05:38,327 --> 00:05:39,290
And then add a function

110
00:05:39,290 --> 00:05:43,090
for initializing the database connection so to say.

111
00:05:43,090 --> 00:05:46,500
And I'll add a database helper variable here,

112
00:05:46,500 --> 00:05:49,923
which will hold the connected database as soon as we got it.

113
00:05:50,760 --> 00:05:52,640
And this will be a async function

114
00:05:52,640 --> 00:05:56,113
because in here, I will use the MongoClient,

115
00:05:57,630 --> 00:06:01,003
which we get from the MongoDB package like this.

116
00:06:01,870 --> 00:06:05,187
MongoClient is mongodb.MongoClient like that.

117
00:06:08,880 --> 00:06:10,220
And then in this async function,

118
00:06:10,220 --> 00:06:14,410
we can await MongoClient.connect,

119
00:06:14,410 --> 00:06:16,550
and then here we have our URL,

120
00:06:16,550 --> 00:06:21,550
which by default is mongodb://localhost:27017.

121
00:06:26,100 --> 00:06:28,493
And this will return us a connected client.

122
00:06:29,440 --> 00:06:32,800
And then we can store the concrete database

123
00:06:32,800 --> 00:06:36,350
to which we wanna talk by calling db on this client,

124
00:06:36,350 --> 00:06:39,268
and to db we pass the name of the database,

125
00:06:39,268 --> 00:06:41,950
and I'll just use first-api here.

126
00:06:41,950 --> 00:06:44,100
The database name, of course, is up to you.

127
00:06:45,370 --> 00:06:47,750
And then I'll add another function

128
00:06:47,750 --> 00:06:50,097
with the name of getDb

129
00:06:50,097 --> 00:06:52,590
where we could, first of all,

130
00:06:52,590 --> 00:06:56,100
check if we maybe don't have a database.

131
00:06:56,100 --> 00:06:59,280
So we tried to call this function too early

132
00:06:59,280 --> 00:07:01,900
in which case, I'll throw a new error

133
00:07:01,900 --> 00:07:05,663
where I say database not connected, for example.

134
00:07:07,350 --> 00:07:09,650
And else if we make it pass this if check,

135
00:07:09,650 --> 00:07:13,680
I return this database helper variable,

136
00:07:13,680 --> 00:07:14,513
and then in the end,

137
00:07:14,513 --> 00:07:17,110
we just have to export these two functions

138
00:07:17,110 --> 00:07:18,590
that we defined here

139
00:07:18,590 --> 00:07:23,100
with module.exports pointing at an object here

140
00:07:23,100 --> 00:07:26,823
where we export initDb like this.

141
00:07:27,950 --> 00:07:31,313
And where we export getDb like this.

142
00:07:33,820 --> 00:07:35,350
So that's this.

143
00:07:35,350 --> 00:07:36,610
And now with this added,

144
00:07:36,610 --> 00:07:40,590
we can go to the model, to the quote.model here,

145
00:07:40,590 --> 00:07:43,703
and in there, define a Quote class,

146
00:07:44,640 --> 00:07:49,640
and already import db by requiring going up one level,

147
00:07:50,730 --> 00:07:54,680
data/database, and in there,

148
00:07:54,680 --> 00:07:59,680
I'll now add a static async method getRandomQuote,

149
00:08:01,520 --> 00:08:04,343
which should do what the name suggests.

150
00:08:05,550 --> 00:08:09,440
And, of course, I'll also already export this Quote class

151
00:08:09,440 --> 00:08:11,823
so that we can use it in other files.

152
00:08:12,660 --> 00:08:14,260
Now, for the logic in here,

153
00:08:14,260 --> 00:08:16,440
what I'll do, and I'll say right away,

154
00:08:16,440 --> 00:08:18,160
this is not the only way of doing it,

155
00:08:18,160 --> 00:08:21,120
but it's hopefully a very understandable way,

156
00:08:21,120 --> 00:08:23,750
what I'll do here is I'll get all my quotes

157
00:08:23,750 --> 00:08:28,413
by awaiting db.getDb collection quotes.

158
00:08:30,630 --> 00:08:33,840
That's where I expect the quotes to be stored.

159
00:08:33,840 --> 00:08:35,700
And we'll have to add them manually

160
00:08:35,700 --> 00:08:37,090
because as mentioned before,

161
00:08:37,090 --> 00:08:40,650
this API won't have any endpoints or methods

162
00:08:40,650 --> 00:08:42,380
for adding quotes.

163
00:08:42,380 --> 00:08:44,820
We'll later build another example API,

164
00:08:44,820 --> 00:08:47,800
which will have all CRUD operations.

165
00:08:47,800 --> 00:08:49,640
This one won't.

166
00:08:49,640 --> 00:08:52,560
So here I just reach out to quotes,

167
00:08:52,560 --> 00:08:54,180
and I wanna find all quotes

168
00:08:54,180 --> 00:08:57,820
by calling find and get them back as an array.

169
00:08:57,820 --> 00:09:00,840
So with that, I'll fetch all the quotes in the database.

170
00:09:00,840 --> 00:09:03,280
And, of course, this is a bit inefficient

171
00:09:03,280 --> 00:09:06,660
if we only wanna get a single random quote,

172
00:09:06,660 --> 00:09:09,660
but again, there would be alternatives with MongoDB

173
00:09:09,660 --> 00:09:13,090
but this approach hopefully is very understandable,

174
00:09:13,090 --> 00:09:15,070
and since the focus of this section here

175
00:09:15,070 --> 00:09:17,920
is not optimizing MongoDB queries,

176
00:09:17,920 --> 00:09:20,070
I would have a whole course about MongoDB

177
00:09:20,070 --> 00:09:22,160
if you would be interested in that.

178
00:09:22,160 --> 00:09:25,617
But instead here, I wanna focus on building APIs

179
00:09:25,617 --> 00:09:28,963
and therefore, I wanna focus on writing understandable code.

180
00:09:30,030 --> 00:09:31,825
So here we get all the quotes.

181
00:09:31,825 --> 00:09:34,320
And now to get a randomQuote,

182
00:09:34,320 --> 00:09:38,010
we can use a object built into JavaScript,

183
00:09:38,010 --> 00:09:39,460
both on the browser side,

184
00:09:39,460 --> 00:09:41,253
as well as on NodeJS.

185
00:09:42,225 --> 00:09:44,160
And that's the Math object.

186
00:09:44,160 --> 00:09:47,390
There we can use the floor method

187
00:09:47,390 --> 00:09:49,950
to round a number down.

188
00:09:49,950 --> 00:09:52,203
So 0.5 would become 0.

189
00:09:52,203 --> 00:09:54,510
1.2 would become 1.

190
00:09:54,510 --> 00:09:56,860
1.8 would also become 1.

191
00:09:56,860 --> 00:09:58,790
That's the idea here.

192
00:09:58,790 --> 00:10:01,230
And the value which I wanna round down

193
00:10:01,230 --> 00:10:04,040
is the result of another calculation

194
00:10:04,040 --> 00:10:06,780
where I use Math.random,

195
00:10:06,780 --> 00:10:08,510
which produces a random number

196
00:10:08,510 --> 00:10:11,890
between zero and one, one excluded.

197
00:10:11,890 --> 00:10:16,303
So it's actually the range zero up to 0.99999.

198
00:10:17,410 --> 00:10:19,610
And I'll multiply this

199
00:10:19,610 --> 00:10:22,580
with quotes.length.

200
00:10:22,580 --> 00:10:25,380
So the number of quotes I have.

201
00:10:25,380 --> 00:10:27,020
Now, to give you an example,

202
00:10:27,020 --> 00:10:32,020
if we had an array with three items,

203
00:10:32,120 --> 00:10:33,530
and yes, these are not quotes

204
00:10:33,530 --> 00:10:34,920
but these are items,

205
00:10:34,920 --> 00:10:36,633
then the length is three.

206
00:10:37,580 --> 00:10:40,000
Keep in mind that the indexes of the items

207
00:10:40,000 --> 00:10:42,260
are zero, one, and two though.

208
00:10:42,260 --> 00:10:44,850
With the formula I have up here,

209
00:10:44,850 --> 00:10:46,860
Math.random could be any value

210
00:10:46,860 --> 00:10:49,480
between zero and one, one excluded.

211
00:10:49,480 --> 00:10:51,203
So it will never be one.

212
00:10:52,070 --> 00:10:54,743
So if Math.random is 0.8, for example,

213
00:10:54,743 --> 00:10:57,330
that would be multiplied with three

214
00:10:57,330 --> 00:11:00,520
and this, of course, would give us 2.4.

215
00:11:00,520 --> 00:11:02,493
Thanks to Math.floor,

216
00:11:03,370 --> 00:11:06,340
2.4 would become 2

217
00:11:07,294 --> 00:11:09,640
and therefore, we would get the item with index two,

218
00:11:09,640 --> 00:11:11,163
which is the last item here.

219
00:11:12,100 --> 00:11:14,173
If this would not be 0.8 but 0.1,

220
00:11:15,170 --> 00:11:18,760
this would, for example, become 0.3 as a result,

221
00:11:18,760 --> 00:11:23,160
and Math.floor of 0.3 would be 0,

222
00:11:23,160 --> 00:11:26,610
so we would select the first item in the array.

223
00:11:26,610 --> 00:11:29,802
And that's what I'm doing here with the quotes array.

224
00:11:29,802 --> 00:11:33,923
And then I'll return that randomQuote which I got here.

225
00:11:35,690 --> 00:11:37,840
Now back in the quotes.controller,

226
00:11:37,840 --> 00:11:41,500
we can, of course, include this Quote class

227
00:11:41,500 --> 00:11:45,273
by requiring it from models/quote.model,

228
00:11:46,170 --> 00:11:49,210
and instead of returning a hard coded quote here,

229
00:11:49,210 --> 00:11:53,430
I got my randomQuote by using await,

230
00:11:53,430 --> 00:11:56,250
and for this, we wanna turn this into async function

231
00:11:56,250 --> 00:11:59,360
as we did many times before in the course.

232
00:11:59,360 --> 00:12:00,410
Quote.getRandomQuote.

233
00:12:03,030 --> 00:12:04,800
And then it's this randomQuote,

234
00:12:04,800 --> 00:12:06,900
which I'll return as a quote here

235
00:12:06,900 --> 00:12:09,573
in my response, in my JSON response.

236
00:12:10,800 --> 00:12:13,350
And still that is basically the only thing

237
00:12:13,350 --> 00:12:16,550
that makes this demo project different compared

238
00:12:16,550 --> 00:12:19,860
to all the other projects we had in this course.

239
00:12:19,860 --> 00:12:21,560
We're not rendering a template.

240
00:12:21,560 --> 00:12:24,810
We have no HTML code and no templates therefore,

241
00:12:24,810 --> 00:12:27,460
and we have no CSS code therefore

242
00:12:27,460 --> 00:12:29,440
but when it comes to the backend logic,

243
00:12:29,440 --> 00:12:31,840
to models and controllers and so on,

244
00:12:31,840 --> 00:12:33,430
it's the same as always.

245
00:12:33,430 --> 00:12:35,084
Nothing is different there.

246
00:12:35,084 --> 00:12:37,320
And that's why it was so important

247
00:12:37,320 --> 00:12:39,240
to use the other approach first

248
00:12:39,240 --> 00:12:42,690
because now you can easily also build APIs

249
00:12:42,690 --> 00:12:45,480
but you can also build regular websites.

250
00:12:45,480 --> 00:12:47,380
Both is possible.

251
00:12:47,380 --> 00:12:49,170
Back to this example though,

252
00:12:49,170 --> 00:12:52,820
with that done, we can go back to app.js

253
00:12:52,820 --> 00:12:53,653
because, of course,

254
00:12:53,653 --> 00:12:56,680
there we have to initialize our database.

255
00:12:56,680 --> 00:13:01,680
So here I'll get my db object by requiring ./data/database.

256
00:13:03,300 --> 00:13:06,970
And then before we start listening on port 3000,

257
00:13:06,970 --> 00:13:09,580
I'll call initDb.

258
00:13:09,580 --> 00:13:11,150
This will return a promise

259
00:13:11,150 --> 00:13:13,520
and since I'm not inside of a function here,

260
00:13:13,520 --> 00:13:16,490
I can't use await here.

261
00:13:16,490 --> 00:13:19,000
In some NodeJS versions, you actually can

262
00:13:19,000 --> 00:13:21,560
but it's safer to just use the other way

263
00:13:21,560 --> 00:13:23,360
of handling promises,

264
00:13:23,360 --> 00:13:25,830
which is to use the then method.

265
00:13:25,830 --> 00:13:28,630
Then pass an anonymous function to it.

266
00:13:28,630 --> 00:13:31,150
And listen inside of this anonymous function,

267
00:13:31,150 --> 00:13:34,950
which will only be executed once this promise here is done.

268
00:13:34,950 --> 00:13:38,260
So once the database connection was established.

269
00:13:38,260 --> 00:13:42,210
And, of course, we can then also add catch here

270
00:13:42,210 --> 00:13:46,380
to catch errors with this anonymous function

271
00:13:46,380 --> 00:13:47,320
and, for example,

272
00:13:47,320 --> 00:13:52,320
console.log connecting to the database failed like this.

273
00:13:55,820 --> 00:13:57,640
Now, we can also, by the way,

274
00:13:57,640 --> 00:14:00,800
add our default error handler,

275
00:14:00,800 --> 00:14:03,450
which we also did before in the course

276
00:14:03,450 --> 00:14:06,010
with this error handling middleware,

277
00:14:06,010 --> 00:14:07,360
which could also be stored

278
00:14:07,360 --> 00:14:09,810
in a separate file in a middlewares folder

279
00:14:09,810 --> 00:14:11,580
if you wanted to.

280
00:14:11,580 --> 00:14:15,300
And then there we could also add error handling.

281
00:14:15,300 --> 00:14:17,050
The difference again would be

282
00:14:17,050 --> 00:14:21,310
that we don't render some 500 template now,

283
00:14:21,310 --> 00:14:24,840
but that instead we would still return some JSON data.

284
00:14:24,840 --> 00:14:26,590
Maybe with an error message,

285
00:14:26,590 --> 00:14:28,060
like something went wrong

286
00:14:28,060 --> 00:14:30,313
or maybe more details if we have them.

287
00:14:31,670 --> 00:14:34,870
We still also wanna set our status code

288
00:14:34,870 --> 00:14:37,280
and with APIs, that status code

289
00:14:37,280 --> 00:14:39,330
is actually even more important

290
00:14:39,330 --> 00:14:42,470
that it was with regular websites.

291
00:14:42,470 --> 00:14:45,040
There it's also recommended to set it

292
00:14:45,040 --> 00:14:49,360
but with APIs, since we're not returning anything visual,

293
00:14:49,360 --> 00:14:53,070
it's even more important to set clear status codes

294
00:14:53,070 --> 00:14:55,520
and return clear data

295
00:14:55,520 --> 00:14:59,580
so that the user, the customer of an API

296
00:14:59,580 --> 00:15:03,640
will be able to understand if a certain action succeeded,

297
00:15:03,640 --> 00:15:06,440
and if it didn't, why it didn't.

298
00:15:06,440 --> 00:15:08,030
So here it's even more important

299
00:15:08,030 --> 00:15:10,330
to set a status code of 500

300
00:15:10,330 --> 00:15:14,440
to indicate that something went wrong on the server side.

301
00:15:14,440 --> 00:15:16,663
And here I need a semicolon.

302
00:15:17,790 --> 00:15:20,800
Okay, so that's the updated app.js file,

303
00:15:20,800 --> 00:15:23,630
updated model and controller and routes.

304
00:15:23,630 --> 00:15:25,189
This should all be connected.

305
00:15:25,189 --> 00:15:27,450
Now, of course, we need quotes

306
00:15:27,450 --> 00:15:29,680
so that fetching a randomQuote actually

307
00:15:29,680 --> 00:15:31,910
has a chance of succeeding.

308
00:15:31,910 --> 00:15:35,080
For this, I opened Mongo shell

309
00:15:35,080 --> 00:15:38,420
and again, I have the MongoDB server up and running.

310
00:15:38,420 --> 00:15:42,180
And I then use this first-api database

311
00:15:42,180 --> 00:15:45,490
because that is the database I'm connecting to here

312
00:15:45,490 --> 00:15:50,490
in my database.js file, first-api.

313
00:15:51,510 --> 00:15:54,110
And now once I'm connected to this database,

314
00:15:54,110 --> 00:15:59,110
I wanna insertOne quote into quotes.

315
00:15:59,260 --> 00:16:02,420
Or actually not just one but I'll use insertMany,

316
00:16:02,420 --> 00:16:05,830
which simply allows me to insert many documents.

317
00:16:05,830 --> 00:16:08,340
You simply pass an array with square brackets

318
00:16:08,340 --> 00:16:11,200
to insertMany, and then your documents

319
00:16:11,200 --> 00:16:15,280
as comma-separated objects so to say.

320
00:16:15,280 --> 00:16:16,970
And then for every quote,

321
00:16:16,970 --> 00:16:20,170
I just wanna have a text field let's say,

322
00:16:20,170 --> 00:16:23,030
which then holds the quote text.

323
00:16:23,030 --> 00:16:27,750
And here I'll just add some dummy quotes

324
00:16:27,750 --> 00:16:31,153
all stored in a text field in these quote documents.

325
00:16:45,750 --> 00:16:47,530
Here we go.

326
00:16:47,530 --> 00:16:48,950
And now I'll hit Enter

327
00:16:48,950 --> 00:16:52,070
and these three documents were now inserted,

328
00:16:52,070 --> 00:16:54,370
and hence if I access my quotes

329
00:16:54,370 --> 00:16:58,010
and find all the documents with the proper syntax,

330
00:16:58,010 --> 00:17:00,210
here they are.

331
00:17:00,210 --> 00:17:01,650
Now with that all done,

332
00:17:01,650 --> 00:17:05,339
back in quote.model, we have one last adjustment to make.

333
00:17:05,339 --> 00:17:07,240
I'm, of course, still returning randomQuote,

334
00:17:07,240 --> 00:17:08,390
which is a number though,

335
00:17:08,390 --> 00:17:09,960
not the actual quote,

336
00:17:09,960 --> 00:17:12,839
so this is actually the randomQuoteIndex

337
00:17:12,839 --> 00:17:15,140
if we wanna label this correctly,

338
00:17:15,140 --> 00:17:17,730
which I want, so I renamed it.

339
00:17:17,730 --> 00:17:21,920
And instead I get the concrete randomQuote now

340
00:17:21,920 --> 00:17:23,560
by accessing quotes

341
00:17:23,560 --> 00:17:26,022
and then the quote for this randomQuoteIndex.

342
00:17:28,000 --> 00:17:29,460
And it's then this randomQuote,

343
00:17:29,460 --> 00:17:30,420
which I wanna return

344
00:17:30,420 --> 00:17:32,620
but also not the quote like this,

345
00:17:32,620 --> 00:17:36,092
but instead, we should access the text property here

346
00:17:36,092 --> 00:17:39,690
because I stored a concrete quote text

347
00:17:39,690 --> 00:17:41,690
under that text property

348
00:17:41,690 --> 00:17:44,270
in those documents in the collection.

349
00:17:44,270 --> 00:17:46,863
Hence we should access the text property here.

350
00:17:47,920 --> 00:17:50,120
In addition, I noticed that in app.js,

351
00:17:50,120 --> 00:17:51,917
I had a tiny mistake.

352
00:17:51,917 --> 00:17:54,020
The prefix for the routes here

353
00:17:54,020 --> 00:17:57,150
should, of course, not be /routes but /quote.

354
00:17:57,150 --> 00:17:59,320
Thinking and talking simultaneously

355
00:17:59,320 --> 00:18:01,740
did not go well together here.

356
00:18:01,740 --> 00:18:03,853
So this should be /quote.

357
00:18:05,200 --> 00:18:06,820
With all that done though,

358
00:18:06,820 --> 00:18:09,710
we should be able to run npm start

359
00:18:09,710 --> 00:18:11,553
thanks to this newly added script.

360
00:18:13,050 --> 00:18:17,170
We now get different quotes here every time we reload.

361
00:18:17,170 --> 00:18:18,470
And it's a random quote

362
00:18:18,470 --> 00:18:21,043
so you can't tell which quote you'll get.

363
00:18:21,880 --> 00:18:25,740
And this is now an API that does a bit more

364
00:18:25,740 --> 00:18:27,960
than it did before but,

365
00:18:27,960 --> 00:18:29,510
and that's really important,

366
00:18:29,510 --> 00:18:32,640
in the end, it's really nothing new.

367
00:18:32,640 --> 00:18:35,390
It is just a controller, a model

368
00:18:35,390 --> 00:18:37,780
and some routes, and the only key difference

369
00:18:37,780 --> 00:18:39,690
is that we never turn views

370
00:18:39,690 --> 00:18:43,040
because we got no views and we want no views.

371
00:18:43,040 --> 00:18:46,600
And we have no front end CSS or JavaScript code.

372
00:18:46,600 --> 00:18:49,620
Instead, all we have here is backend code

373
00:18:49,620 --> 00:18:51,230
and we exchange data.

374
00:18:51,230 --> 00:18:53,013
We return JSON data.

375
00:18:54,520 --> 00:18:57,050
Now, to finish this, of course,

376
00:18:57,050 --> 00:18:59,910
we could also add error handling here therefore,

377
00:18:59,910 --> 00:19:02,470
just as we did it before in the course

378
00:19:02,470 --> 00:19:06,300
because all these concepts you learned about didn't change.

379
00:19:06,300 --> 00:19:08,820
So when you work with a database,

380
00:19:08,820 --> 00:19:11,730
you might want to try catch this,

381
00:19:11,730 --> 00:19:15,030
and then call return next error here

382
00:19:15,030 --> 00:19:19,123
to forward this error to Express.js error handler.

383
00:19:20,030 --> 00:19:22,340
And since randomQuote is now scoped

384
00:19:22,340 --> 00:19:25,190
to the try block but I wanna use it thereafter,

385
00:19:25,190 --> 00:19:28,390
we now either have to move this code here

386
00:19:28,390 --> 00:19:31,583
into the try block so that it has the same scope

387
00:19:31,583 --> 00:19:34,030
or we keep it here

388
00:19:34,030 --> 00:19:37,770
and we then add this as a variable, the randomQuote,

389
00:19:37,770 --> 00:19:41,090
and we set this variable inside of the try block.

390
00:19:41,090 --> 00:19:43,283
Both solutions will do the trick.

391
00:19:44,330 --> 00:19:46,630
Now, this won't change the behavior here

392
00:19:46,630 --> 00:19:48,920
but again, it is important to understand

393
00:19:48,920 --> 00:19:52,220
that you build a regular web backend.

394
00:19:52,220 --> 00:19:53,190
The key difference

395
00:19:53,190 --> 00:19:55,730
is that we don't have any front end code though

396
00:19:55,730 --> 00:19:58,023
but that instead we exchange data.

