1
00:00:00,340 --> 00:00:01,590
OK we'll come back.

2
00:00:01,710 --> 00:00:03,160
It's time for another exercise.

3
00:00:03,270 --> 00:00:08,430
This time we're going to get practice creating an express app from scratch using NPM and NPM and net

4
00:00:08,640 --> 00:00:13,630
and a package that Jason and most importantly you'll get practice writing routes in Express.

5
00:00:13,920 --> 00:00:17,360
So I have written out some instructions but I'm also going to demonstrate how it works.

6
00:00:17,370 --> 00:00:19,500
I think they will make it a little clearer.

7
00:00:19,530 --> 00:00:22,440
You'll need to create a brand new Express app from scratch.

8
00:00:22,590 --> 00:00:28,350
So make a new directory name it whatever you want and then inside of that you need to use NPM and that

9
00:00:28,620 --> 00:00:33,900
follow all the instructions to create a package that Jay song and don't need to install Express.

10
00:00:34,020 --> 00:00:39,660
And I want you to save it as a dependency to the package of a song and then you need to set up the Express

11
00:00:39,660 --> 00:00:40,140
up.

12
00:00:40,140 --> 00:00:44,760
You may need to look at some old videos or some old code of ours to remind yourself how that works.

13
00:00:44,880 --> 00:00:45,990
But you'll import it.

14
00:00:46,110 --> 00:00:50,610
You need to make sure that the end of the file you're starting the server and then you need three different

15
00:00:50,610 --> 00:00:51,300
routes.

16
00:00:51,630 --> 00:00:54,210
The first one is pretty straightforward.

17
00:00:54,210 --> 00:00:58,140
Whenever a user visits slash it should print either.

18
00:00:58,140 --> 00:00:59,730
Welcome to my assignment.

19
00:00:59,760 --> 00:01:04,840
The next one's a little bit more complicated when a user visits slash speak slash pig.

20
00:01:04,920 --> 00:01:07,590
It should print out the pig says oink.

21
00:01:07,590 --> 00:01:11,260
Same thing for cow except it should print out the cow says moo.

22
00:01:11,340 --> 00:01:16,060
And for Slash speak slash dog should print out the dog says Wolf Wolf.

23
00:01:16,310 --> 00:01:18,520
And that needs to work for at least five animals.

24
00:01:18,630 --> 00:01:19,850
And you can pick the other two.

25
00:01:20,010 --> 00:01:23,390
But the most important part is that this is one route definition.

26
00:01:23,400 --> 00:01:29,560
So you're not writing five different apt get for pig cow and dog you're doing it all in one route.

27
00:01:29,910 --> 00:01:35,370
And the less starts a little bit more complicated what it will do is take a word like hello and a number

28
00:01:35,750 --> 00:01:38,280
and it will repeat hello three times.

29
00:01:38,280 --> 00:01:43,770
In this case and this case it should repeat Hello five times and that's what we see here.

30
00:01:43,920 --> 00:01:46,070
And there are spaces between the words.

31
00:01:46,350 --> 00:01:51,230
And in this case it will repeat blaw two times which will print blah blah.

32
00:01:51,480 --> 00:01:57,420
So we have repeat slash a word slash the number of times to repeat the word.

33
00:01:57,420 --> 00:02:00,220
I'll give you a few hints on what you need to think about.

34
00:02:00,240 --> 00:02:06,810
The first is that when you get this number out of the path it's actually going to be a string and you

35
00:02:06,810 --> 00:02:09,210
need to turn it into a number to work with it.

36
00:02:09,510 --> 00:02:15,020
And the second one is that you're going to want to use a loop to build up a string to printout Musli

37
00:02:15,140 --> 00:02:15,510
.

38
00:02:15,750 --> 00:02:21,630
If a user visits any other route that you haven't defined You should print out sorry page not found

39
00:02:21,640 --> 00:02:21,770
.

40
00:02:21,900 --> 00:02:24,040
What are you doing with your life.

41
00:02:24,060 --> 00:02:26,670
The last thing I'll do is give you a brief demonstration.

42
00:02:26,730 --> 00:02:28,440
So I have a solution written up.

43
00:02:28,680 --> 00:02:29,740
I'll just run it.

44
00:02:30,030 --> 00:02:31,670
Node Aptera Yes.

45
00:02:31,760 --> 00:02:36,630
And now if I go to these routes the first one is just the route route I get.

46
00:02:36,630 --> 00:02:37,150
Hi there.

47
00:02:37,170 --> 00:02:38,800
Welcome to my assignment.

48
00:02:38,850 --> 00:02:43,230
Next if I go to slash speak slash cow.

49
00:02:43,620 --> 00:02:44,630
I get mu.

50
00:02:44,850 --> 00:02:46,390
I go to speak sashed pig.

51
00:02:46,580 --> 00:02:49,080
I get oink and so on.

52
00:02:49,260 --> 00:02:59,640
And then if I go to slash repeat and all do dog and or repeat it 10 times I get dog repeated 10 times

53
00:03:00,090 --> 00:03:06,440
or I could even do it a thousand times and I can change the word so that it's now Wolf.

54
00:03:07,200 --> 00:03:08,220
And there we go.

55
00:03:08,730 --> 00:03:14,700
And then there's also the catch all which if I go to any route that isn't defined we get sorry.

56
00:03:14,700 --> 00:03:15,840
Page Not Found.

57
00:03:15,870 --> 00:03:17,860
What are you doing with your life.

58
00:03:17,910 --> 00:03:18,600
All right.

59
00:03:18,600 --> 00:03:23,920
So as always I highly highly recommend that you do this exercise don't just fast forward.

60
00:03:23,970 --> 00:03:25,410
Don't just skip to the next video.

61
00:03:25,590 --> 00:03:30,030
Even though it's not a glamorous beautiful app it's really important that you get these concepts down

62
00:03:30,330 --> 00:03:35,340
that you can create an app from scratch that you can use NPM and create your package that Jason that

63
00:03:35,340 --> 00:03:40,320
you can set up routes on your own that used path variables and everything else that's associated with

64
00:03:40,320 --> 00:03:41,150
Express.

65
00:03:41,330 --> 00:03:45,120
I just need you to keep getting more and more familiar with the workflow and the best way for you to

66
00:03:45,120 --> 00:03:46,910
do that is to just practice.

67
00:03:46,920 --> 00:03:50,910
It's one thing to watch a video and see me do it but it's a totally different animal.

68
00:03:50,920 --> 00:03:53,520
Have you actually do it yourself and you'll learn a lot more.

69
00:03:53,610 --> 00:03:54,900
So please do it.

70
00:03:54,900 --> 00:03:55,920
Don't cheat.

71
00:03:55,920 --> 00:03:56,210
All right.

72
00:03:56,220 --> 00:03:57,680
I'll see you with the solution up next.
