1
00:00:01,877 --> 00:00:05,494
Now, let's dig a bit deeper into Node.JS.

2
00:00:05,494 --> 00:00:08,076
We've got some basic Node.JS code here,

3
00:00:08,076 --> 00:00:11,117
but of course this code isn't doing too much.

4
00:00:11,117 --> 00:00:16,117
What I wanna do now is I wanna support a brand new path

5
00:00:18,550 --> 00:00:19,383
in my URL.

6
00:00:19,383 --> 00:00:20,840
To be precise,

7
00:00:20,840 --> 00:00:24,560
I wanna make sure that if we enter our domain,

8
00:00:24,560 --> 00:00:26,247
so in our case, localhost,

9
00:00:26,247 --> 00:00:29,767
and then the port, /currenttime,

10
00:00:30,991 --> 00:00:34,653
I wanna show a current timestamp on the page.

11
00:00:34,653 --> 00:00:39,600
If we enter just localhost:3000.

12
00:00:39,600 --> 00:00:43,629
So basically, localhost:3000/ nothing, you could say.

13
00:00:43,629 --> 00:00:45,832
The slash doesn't really make a difference here.

14
00:00:45,832 --> 00:00:50,200
Then, I instead want to show this Hello World!

15
00:00:50,200 --> 00:00:51,235
or welcome page.

16
00:00:51,235 --> 00:00:55,730
So I wanna send back two different kinds

17
00:00:55,730 --> 00:01:00,730
of responses based on the path, as this part is called here,

18
00:01:01,580 --> 00:01:03,400
that is entered in the URL.

19
00:01:09,200 --> 00:01:13,810
Now, of course, we did already send back different responses

20
00:01:13,810 --> 00:01:16,440
before in the course when we worked

21
00:01:16,440 --> 00:01:18,890
with different HTML files.

22
00:01:18,890 --> 00:01:22,570
Then, we were also able to enter slash

23
00:01:22,570 --> 00:01:27,570
and then the HTML file name to load different HTML files.

24
00:01:28,506 --> 00:01:33,506
But the whole idea of getting started with Node.JS is

25
00:01:33,674 --> 00:01:38,164
that we see that we can now actually have full control

26
00:01:38,164 --> 00:01:40,926
over the response that is being sent back

27
00:01:40,926 --> 00:01:45,226
and that we no longer need to create HTML files

28
00:01:45,226 --> 00:01:48,746
as responses, but that we can control the response

29
00:01:48,746 --> 00:01:51,908
with Node.JS and JavaScript only.

30
00:01:51,908 --> 00:01:55,020
And then, in the next sections,

31
00:01:55,020 --> 00:01:58,329
we are going to reintroduce HTML files,

32
00:01:58,329 --> 00:02:00,909
but then we'll also write them such

33
00:02:00,909 --> 00:02:04,404
that they are a bit more dynamic, but more on that later.

34
00:02:04,404 --> 00:02:08,051
So to handle these two different paths here

35
00:02:08,051 --> 00:02:09,889
in this handler request function,

36
00:02:09,889 --> 00:02:14,890
we now can extract some extra data from this request object,

37
00:02:15,680 --> 00:02:17,240
which we get automatically.

38
00:02:17,240 --> 00:02:22,240
This request object actually has a URL property.

39
00:02:23,204 --> 00:02:26,925
It will have that URL property because this object,

40
00:02:26,925 --> 00:02:30,710
which we get is created by Node.JS,

41
00:02:30,710 --> 00:02:34,319
and it is created such that it has such a URL property.

42
00:02:34,319 --> 00:02:39,319
And the URL property actually will not hold the full URL,

43
00:02:39,498 --> 00:02:41,667
but it will hold this part.

44
00:02:41,667 --> 00:02:46,053
So the part after our domain and port.

45
00:02:46,053 --> 00:02:51,053
And therefore, we can now use this request.url property

46
00:02:52,060 --> 00:02:55,979
in an if statement and check if request.url is equal

47
00:02:55,979 --> 00:03:00,874
to /currenttime so that we then,

48
00:03:00,874 --> 00:03:03,140
inside of this if statement,

49
00:03:03,140 --> 00:03:05,156
can write the code that should execute

50
00:03:05,156 --> 00:03:08,634
if a request is sent to that URL.

51
00:03:08,634 --> 00:03:13,634
And else, if a request is sent to a different URL,

52
00:03:14,050 --> 00:03:16,815
or maybe else if, if we wanna be more specific,

53
00:03:16,815 --> 00:03:20,060
if request.url is equal to just /,

54
00:03:20,060 --> 00:03:25,060
so just our domain with nothing after it,

55
00:03:25,700 --> 00:03:27,370
that's represented by just /,

56
00:03:27,370 --> 00:03:30,384
then I wanna execute different code.

57
00:03:30,384 --> 00:03:35,000
And then, we could move this code here and move that

58
00:03:35,000 --> 00:03:39,908
into this else-if branch and auto format this code.

59
00:03:39,908 --> 00:03:43,372
And here I can still say Hello World!

60
00:03:43,372 --> 00:03:47,354
But then, I'll also copy this and added here inside

61
00:03:47,354 --> 00:03:49,550
of this first bronch.

62
00:03:49,550 --> 00:03:53,840
And then, here, I can sent back a different piece

63
00:03:53,840 --> 00:03:54,800
of information.

64
00:03:54,800 --> 00:03:57,695
I might still want to create a h1 tag here,

65
00:03:57,695 --> 00:04:00,920
but I actually wanna create it such

66
00:04:00,920 --> 00:04:04,449
that I can insert some dynamic data into it,

67
00:04:04,449 --> 00:04:07,460
because that's one of the main reasons

68
00:04:07,460 --> 00:04:12,178
for using Node.JS, or for using a programming language

69
00:04:12,178 --> 00:04:15,175
that executes on the remote machine.

70
00:04:15,175 --> 00:04:19,510
I want to be able to send back different responses

71
00:04:19,510 --> 00:04:24,510
that might contain dynamically fetched or generated data.

72
00:04:24,595 --> 00:04:26,850
And that's what I wanna simulate here.

73
00:04:26,850 --> 00:04:30,210
Between the opening and closing h1 tag here,

74
00:04:30,210 --> 00:04:34,966
in this if branch, I want to output the current timestamp.

75
00:04:34,966 --> 00:04:39,506
And I can do this by injecting something

76
00:04:39,506 --> 00:04:43,400
between these two strings, a third string,

77
00:04:43,400 --> 00:04:45,010
which is the current timestamp.

78
00:04:45,010 --> 00:04:50,010
And in JavaScript, both on the browser side and in Node.JS,

79
00:04:50,686 --> 00:04:55,686
we can get that current timestamp by using new Date,

80
00:04:55,783 --> 00:04:59,158
which creates a date object.

81
00:04:59,158 --> 00:05:02,977
And on this created new Date object,

82
00:05:02,977 --> 00:05:07,977
we can call toISOString to convert this date object

83
00:05:09,770 --> 00:05:14,340
into a string representation, so into some readable text.

84
00:05:14,340 --> 00:05:17,760
toISOString is a method which we can call

85
00:05:17,760 --> 00:05:20,882
on that created new Date object.

86
00:05:20,882 --> 00:05:24,190
And new Date simply is a built-in way of creating

87
00:05:24,190 --> 00:05:25,294
such a date object.

88
00:05:25,294 --> 00:05:29,213
Now with that, if we save everything,

89
00:05:29,213 --> 00:05:34,080
we have two different URLs we can target

90
00:05:34,080 --> 00:05:35,812
and we'll get different responses.

91
00:05:35,812 --> 00:05:39,167
So if we now open this terminal again,

92
00:05:39,167 --> 00:05:41,391
and here I did that with the shortcut,

93
00:05:41,391 --> 00:05:43,668
you can, of course, still also do it manually.

94
00:05:43,668 --> 00:05:46,870
You can see the shortcut if you go to Preferences,

95
00:05:46,870 --> 00:05:48,203
Keyboard Shortcuts.

96
00:05:49,460 --> 00:05:53,690
Here, I'll then again execute this app.js file

97
00:05:53,690 --> 00:05:55,247
with node app.js.

98
00:05:55,247 --> 00:05:58,244
This process then keeps on running as learned.

99
00:05:58,244 --> 00:06:01,997
And now, if I visit localhost:3000,

100
00:06:01,997 --> 00:06:04,210
I still get Hello World!,

101
00:06:04,210 --> 00:06:07,705
because that is basically localhost:3000/ nothing.

102
00:06:07,705 --> 00:06:10,981
And that is this second branch here.

103
00:06:10,981 --> 00:06:15,981
If I enter localhost:3000/currenttime though,

104
00:06:17,980 --> 00:06:21,330
then I get this current time snapshot.

105
00:06:21,330 --> 00:06:24,460
Of course, it's not formatted in a super pretty way,

106
00:06:24,460 --> 00:06:26,530
but it's good enough for the moment

107
00:06:26,530 --> 00:06:28,483
and it proves that this works.

108
00:06:28,483 --> 00:06:32,677
If I enter anything else, like /something,

109
00:06:32,677 --> 00:06:35,161
instead, we'll get an error.

110
00:06:35,161 --> 00:06:38,324
This will keep on loading, and ultimately, it will crash,

111
00:06:38,324 --> 00:06:40,650
because this function executes

112
00:06:40,650 --> 00:06:43,311
but never produces a valid response.

113
00:06:43,311 --> 00:06:45,204
And therefore, ultimately,

114
00:06:45,204 --> 00:06:49,831
this request will time out because it never gets a response.

115
00:06:49,831 --> 00:06:53,220
You might wanna handle such scenarios as well,

116
00:06:53,220 --> 00:06:56,523
but here we'll just ignore it and focus on the two cases

117
00:06:56,523 --> 00:06:58,033
that do work.

