Professional Documents
Culture Documents
1
00:00:00.710 --> 00:00:04.900
In this exercise we're going to make
use of the Express Generator to quickly
2
00:00:04.900 --> 00:00:07.800
scaffold out and express application.
3
00:00:07.800 --> 00:00:10.790
We'll replicate
the functionality that we did
4
00:00:10.790 --> 00:00:14.420
with the Express application
in the previous module.
5
00:00:14.420 --> 00:00:20.800
And support that REST API with
multiple application draws.
6
00:00:20.800 --> 00:00:24.487
So, this way you will learn how
to make use of the generator
7
00:00:24.487 --> 00:00:27.660
to quickly scaffold out
an Express application.
8
00:00:29.660 --> 00:00:34.000
The first thing that we need to
do to get started is to install
9
00:00:34.000 --> 00:00:39.170
the Express Generator as
a global npm package.
10
00:00:39.170 --> 00:00:42.971
So to do that at the command prompt,
11
00:00:42.971 --> 00:00:48.410
type npm install express-generator -g.
12
00:00:48.410 --> 00:00:53.361
So let's do that and then once it is
installed then we will scaffold out and
13
76
00:06:06.702 --> 00:06:11.840
This express.static,
we have seen already in
77
00:06:11.840 --> 00:06:18.010
the previous module for
serving up static data.
78
00:06:18.010 --> 00:06:21.770
Also note the use of
path.join method there.
79
00:06:21.770 --> 00:06:26.910
So join is one of the path
methods that joins dirname with
80
00:06:26.910 --> 00:06:32.120
the public folder to create
the entire path from which
81
00:06:32.120 --> 00:06:38.310
the folder which contains
those static files.
82
00:06:38.310 --> 00:06:43.030
So this will end up generating,
referring to the /public folder that is
83
00:06:43.030 --> 00:06:47.720
part of this Express
application folder thing.
84
00:06:47.720 --> 00:06:51.740
Also note how we declare app.use/routes,
85
00:06:51.740 --> 00:06:56.921
meaning that any request to
the URL that ends with a slash,
86
00:06:56.921 --> 00:07:01.279
and the beginning will be
handled by the routes.
87
00:07:04.530 --> 00:07:09.602
File in there, meaning that
when you specify routes there,
88
00:07:09.602 --> 009:16.900 --> 00:19:21.580
I'm going to copy the AboutUs.html
in the index.html file from there.
246
00:19:22.800 --> 00:19:28.784
And then copy them to that node
express gems public folder.
247
00:19:28.784 --> 00:19:32.235
So I'm going to copy those two in here.
248
00:19:32.235 --> 00:19:36.935
So which means that if I issue
a requestor index these two
249
00:19:36.935 --> 00:19:41.699
should be served by this
express gem router also.
250
00:19:43.890 --> 00:19:44.820
Now.
251
00:19:44.820 --> 00:19:50.210
Now that we have completed that part,
we need to fix up the app .js file so
252
00:19:50.210 --> 00:19:57.330
that it uses the three new routers,
routers that we have developed.
253
00:19:57.330 --> 00:20:01.478
And then be able to handle those requests,
254
00:20:01.478 --> 00:20:08.230
the rest API-based requests
to those particular URIs.
255
00:20:08.230 --> 00:20:12.538
So /dishes/dishes/dishID or
256
00:20:12.538 --> 00:20:16.370
/ Promotions and slash leadership also.
257
00:20:16.370 --> 00:20:21.020
So to do that,
of course we need to fix the app.js file.
258
00:20:21.020 --> 00:20:24.170
So let's go ahead and
fix up the app.js file.
259
00:20:25.990 --> 00:20:30.625
Going to the app.js file,
I'm going to now require the remaining
260
00:20:30.625 --> 00:20:35.590
three Routes that I have
just included there.
261
00:20:35.590 --> 00:20:37.530
So I would say var,
262
00:20:42.335 --> 00:20:45.692
DishRouter.
263
00:20:45.692 --> 00:20:48.005
That is what we use there.
264
00:20:48.005 --> 00:20:51.959
So we'll say require
265
00:20:51.959 --> 00:20:57.235
./routes/ dishRouter.
266
00:21:01.155 --> 00:21:01.770
Similarly.
267
00:21:07.471 --> 00:21:08.608
Routes.
268
00:21:11.998 --> 00:21:17.238
PromoRouter, you should have implemented
this as part of your first assignment.
269
00:21:17.238 --> 00:21:26.746
Similarly Dot,
270
00:21:26.746 --> 00:21:30.496
slash, dot, slash.
271
00:21:34.826 --> 00:21:39.664
So after this we now need to mount
272
00:21:39.664 --> 00:21:44.503
these at the appropriate URLs so
273
00:21:44.503 --> 00:21:50.460
that these three routers can be used.
274
00:21:50.460 --> 00:21:55.150
So to do that we'll just browse
down to where we use this app.
275
00:21:55.150 --> 00:21:55.960
Use.
276
00:21:55.960 --> 00:22:00.257
So I'm going to mount them as app.use,
277
00:22:04.001 --> 00:22:06.138
Dishes.
278
00:22:06.138 --> 00:22:09.608
That's where the dish
router takes care of it.
279
00:22:18.744 --> 00:22:20.323
Promotions.
280
00:22:22.820 --> 00:22:25.002
PromoRouter takes care of that.
281
00:22:25.002 --> 00:22:33.883
And then Leadership should be handled by
282
00:22:36.517 --> 00:22:39.249
LeaderRouter.
283
00:22:39.249 --> 00:22:40.867
That's it.
284
00:22:40.867 --> 00:22:46.980
Our express generated application should
now be able to handle all those requests.
285
00:22:46.980 --> 00:22:48.580
Just like before.
286
299
00:23:54.050 --> 00:23:58.070
servers are able to serve
up the starting resources.
300
00:23:58.070 --> 00:24:04.750
Now let's start sending requests
to local host 3000 dishes,
301
00:24:04.750 --> 00:24:09.770
and then it says we'll send
all the dishes to you.
302
00:24:09.770 --> 00:24:17.200
We'll say post to localhost:3000/dishes
and then send the request.
303
00:24:17.200 --> 00:24:21.680
And then you notice that it is
sending back saying we'll add
304
00:24:21.680 --> 00:24:25.450
the dish:newDish with details:
some detailed description.
305
00:24:25.450 --> 00:24:30.809
Let's do a delete and
then it should send deleting all dishes.
306
00:24:31.890 --> 00:24:38.380
Let's do a delete on dishes/2.
307
00:24:38.380 --> 00:24:41.380
Which is deleting dish 2.
308
00:24:41.380 --> 00:24:45.640
Let's do a put 2 dishes/2.
309
00:24:45.640 --> 00:24:50.630
And then it should, see,
updating dish 2 just like before.
310
00:24:50.630 --> 00:24:55.370
So all this part is
working just like before.
311
00:24:55.370 --> 00:25:00.030