1
00:00:02,270 --> 00:00:03,210
<v Instructor>So let's get started</v>

2
00:00:03,210 --> 00:00:05,210
with some Standard Libraries.

3
00:00:05,210 --> 00:00:09,130
And for that, I'm actually going to work with

4
00:00:09,130 --> 00:00:11,220
the HTTP library.

5
00:00:11,220 --> 00:00:15,140
That library will help us with spinning up a web server,

6
00:00:15,140 --> 00:00:17,420
and actually here in the docs of the library,

7
00:00:17,420 --> 00:00:21,160
we have some dummy code for doing that.

8
00:00:21,160 --> 00:00:23,680
So in the end, we're just going to replicate that code,

9
00:00:23,680 --> 00:00:26,970
but I'm going to explain what's happening here.

10
00:00:26,970 --> 00:00:30,920
Now we see one important difference to note right away

11
00:00:30,920 --> 00:00:33,040
and that's the import syntax.

12
00:00:33,040 --> 00:00:36,390
Now, I don't just mean the import from syntax,

13
00:00:36,390 --> 00:00:39,810
which differs from the const require import syntax

14
00:00:39,810 --> 00:00:41,460
we used throughout this course,

15
00:00:41,460 --> 00:00:45,300
but I also mean, from where we are importing.

16
00:00:45,300 --> 00:00:47,240
That's a URL.

17
00:00:47,240 --> 00:00:51,500
As you learned, Node does support the import from syntax.

18
00:00:51,500 --> 00:00:54,060
I do cover that in the modern JavaScript

19
00:00:54,060 --> 00:00:56,310
with Node core section.

20
00:00:56,310 --> 00:01:00,710
But what Node doesn't support, are URL imports.

21
00:01:00,710 --> 00:01:05,470
Where we don't import from a local file or a module,

22
00:01:05,470 --> 00:01:08,810
but where we instead point at a file on another server

23
00:01:08,810 --> 00:01:10,400
to import that file.

24
00:01:10,400 --> 00:01:13,003
And that's something Deno does.

25
00:01:13,003 --> 00:01:17,260
Deno wants you to use such imports.

26
00:01:17,260 --> 00:01:21,250
The idea with Deno is not that you download everything

27
00:01:21,250 --> 00:01:24,720
you're going to use in your project and store it locally,

28
00:01:24,720 --> 00:01:27,680
but then instead, you can import from any JavaScript

29
00:01:27,680 --> 00:01:30,410
or TypeScript file on any server

30
00:01:30,410 --> 00:01:34,433
and start using those exported features in your files.

31
00:01:35,590 --> 00:01:37,850
This is a core philosophy of Deno

32
00:01:37,850 --> 00:01:40,820
and that's, of course quite different to Node.

33
00:01:40,820 --> 00:01:44,480
So therefore, here, if we want to use this standard library,

34
00:01:44,480 --> 00:01:47,740
the HTTP library, and from that library,

35
00:01:47,740 --> 00:01:52,100
the serve function exported from the server.ts file,

36
00:01:52,100 --> 00:01:55,510
then we have to go into our app.ts file

37
00:01:55,510 --> 00:01:57,420
and add this code in here.

38
00:01:57,420 --> 00:01:58,950
And this will then do just that.

39
00:01:58,950 --> 00:02:03,200
It will import a function from this remote file,

40
00:02:03,200 --> 00:02:05,600
and of course you can only import what's exported

41
00:02:05,600 --> 00:02:08,240
if we dive into the server.ts file though,

42
00:02:08,240 --> 00:02:11,663
we see that if we search for serve,

43
00:02:17,830 --> 00:02:19,820
If we search long enough, at least

44
00:02:22,750 --> 00:02:25,670
we have the exported serve function here,

45
00:02:25,670 --> 00:02:27,440
because it's exported here,

46
00:02:27,440 --> 00:02:29,453
we can import it with this code.

47
00:02:30,490 --> 00:02:33,670
So, this is how we import and we don't need to download

48
00:02:33,670 --> 00:02:38,410
or NPM install this, it's also not built into Deno

49
00:02:38,410 --> 00:02:41,510
so it's also not like the file system module,

50
00:02:41,510 --> 00:02:43,020
which comes with Node.

51
00:02:43,020 --> 00:02:45,430
Instead, it simply sits on another server

52
00:02:45,430 --> 00:02:47,490
and this is how we import it.

53
00:02:47,490 --> 00:02:49,520
Now serve is then a function

54
00:02:49,520 --> 00:02:53,390
that allows us to spin up a HTTP server.

55
00:02:53,390 --> 00:02:56,520
And we spin up that server by calling serve,

56
00:02:56,520 --> 00:02:59,850
and then through serve, we pass an object to configure it

57
00:02:59,850 --> 00:03:01,890
and in this object, we can, for example,

58
00:03:01,890 --> 00:03:04,360
specify the port on which we wanna listen.

59
00:03:04,360 --> 00:03:07,890
And here for development, we could do this on port 3000.

60
00:03:07,890 --> 00:03:10,793
We do this with the port property in this object.

61
00:03:11,870 --> 00:03:14,020
Now server then returns a server,

62
00:03:14,020 --> 00:03:16,580
which we can store in a constant.

63
00:03:16,580 --> 00:03:18,120
And now this server,

64
00:03:18,120 --> 00:03:21,080
of course could have incoming requests.

65
00:03:21,080 --> 00:03:25,550
In Node.js, we would now specify a callback function

66
00:03:25,550 --> 00:03:28,050
that executes for every incoming request.

67
00:03:28,050 --> 00:03:32,560
Deno instead embraces modern JavaScript features

68
00:03:32,560 --> 00:03:35,070
for all its core capabilities,

69
00:03:35,070 --> 00:03:37,700
and the server is no exception.

70
00:03:37,700 --> 00:03:39,020
In this example, snippet,

71
00:03:39,020 --> 00:03:43,280
we see how we listen to incoming requests in Deno

72
00:03:43,280 --> 00:03:47,260
with this strange construct here.

73
00:03:47,260 --> 00:03:50,000
Now for await of,

74
00:03:50,000 --> 00:03:52,110
is a JavaScript feature,

75
00:03:52,110 --> 00:03:54,130
it's not Deno exclusive.

76
00:03:54,130 --> 00:03:58,810
Instead, it's a JavaScript feature called async iterable

77
00:03:58,810 --> 00:04:02,690
Server, is this so called asynchronous iterable,

78
00:04:02,690 --> 00:04:07,140
which simply means it's like an array full of promises

79
00:04:07,140 --> 00:04:09,770
and it's an infinite array basically.

80
00:04:09,770 --> 00:04:12,930
So this server generates new promises

81
00:04:12,930 --> 00:04:15,070
which we can await like this,

82
00:04:15,070 --> 00:04:16,580
and the new promise,

83
00:04:16,580 --> 00:04:18,480
which then resolves, is generated

84
00:04:18,480 --> 00:04:20,300
for every incoming request.

85
00:04:20,300 --> 00:04:23,990
That's what server does under the hood, the Deno server.

86
00:04:23,990 --> 00:04:27,410
And you could build this entirely with vanilla JavaScript

87
00:04:27,410 --> 00:04:31,033
async iterables are vanilla JavaScript.

88
00:04:31,970 --> 00:04:34,920
Attached, you'll find a link to the MDN documentation

89
00:04:34,920 --> 00:04:37,700
for async iterables, in case you're interested

90
00:04:37,700 --> 00:04:40,090
in learning a bit more about them.

91
00:04:40,090 --> 00:04:42,780
So, if I copy this code here,

92
00:04:42,780 --> 00:04:46,880
into my app.ts file, we'll execute this code

93
00:04:46,880 --> 00:04:48,990
for every incoming request.

94
00:04:48,990 --> 00:04:53,040
And this is just a Deno's way of sending back a response

95
00:04:53,040 --> 00:04:56,210
on the request object which is generated

96
00:04:56,210 --> 00:04:57,990
for every incoming request.

97
00:04:57,990 --> 00:05:00,190
We got a respond method,

98
00:05:00,190 --> 00:05:03,940
which wants an object, where we can specify a body

99
00:05:03,940 --> 00:05:07,560
for the response, which will be sent back by Deno.

100
00:05:07,560 --> 00:05:09,630
So this is how we listen to requests,

101
00:05:09,630 --> 00:05:13,700
then do whatever we wanna do, and then respond back.

102
00:05:13,700 --> 00:05:16,350
And one noteworthy thing here,

103
00:05:16,350 --> 00:05:21,250
is that we have await here, on the top level like this.

104
00:05:21,250 --> 00:05:24,870
This is not wrapped in any asynchronous function

105
00:05:24,870 --> 00:05:27,310
as you often needed in Avro code,

106
00:05:27,310 --> 00:05:32,220
instead Deno out of the box supports top level await.

107
00:05:32,220 --> 00:05:34,650
So that you can use the await keyword

108
00:05:34,650 --> 00:05:37,050
outside of asynchronous functions.

109
00:05:37,050 --> 00:05:39,440
In modern versions of Node.js,

110
00:05:39,440 --> 00:05:41,450
this is also supported though,

111
00:05:41,450 --> 00:05:43,563
so that's not really a key difference.

112
00:05:44,440 --> 00:05:47,790
Well, with that, if we save this,

113
00:05:47,790 --> 00:05:51,460
if I now run this file with Deno run app.ts,

114
00:05:51,460 --> 00:05:55,280
it compiles and it will ultimately crash,

115
00:05:55,280 --> 00:05:56,750
but before it crashes,

116
00:05:56,750 --> 00:05:59,540
you see it downloads a bunch of things.

117
00:05:59,540 --> 00:06:02,460
And that happens because of this import.

118
00:06:02,460 --> 00:06:05,460
Whenever you import from a URL like this,

119
00:06:05,460 --> 00:06:07,016
when you execute the code,

120
00:06:07,016 --> 00:06:10,120
Deno goes ahead and reaches out to that server

121
00:06:10,120 --> 00:06:11,880
and downloads that file

122
00:06:11,880 --> 00:06:14,640
and of course, also dependencies of that file.

123
00:06:14,640 --> 00:06:18,370
So, all other files which are imported by that file

124
00:06:18,370 --> 00:06:21,600
and it downloads those files to your local machine,

125
00:06:21,600 --> 00:06:23,700
and caches them locally.

126
00:06:23,700 --> 00:06:26,410
so that when you run the script thereafter,

127
00:06:26,410 --> 00:06:28,460
it doesn't have to re-download them

128
00:06:28,460 --> 00:06:30,840
simply to speed up execution time,

129
00:06:30,840 --> 00:06:34,690
and to make sure it doesn't exhaust all your bandwidth.

130
00:06:34,690 --> 00:06:38,200
With this approach, you can also develop on a plane,

131
00:06:38,200 --> 00:06:41,740
assuming that you did pre-download all the files

132
00:06:41,740 --> 00:06:42,993
you'll eventually need.

133
00:06:45,970 --> 00:06:48,870
Now, after it did all that, it crashes however,

134
00:06:48,870 --> 00:06:52,110
and it again crashes because of a permission error

135
00:06:52,110 --> 00:06:55,040
that network access was denied.

136
00:06:55,040 --> 00:06:57,170
And this makes sense because I mentioned

137
00:06:57,170 --> 00:07:00,400
that as part of Deno's permission model

138
00:07:00,400 --> 00:07:04,100
some things, like writing or reading to files,

139
00:07:04,100 --> 00:07:08,540
but also network requests are blocked by default.

140
00:07:08,540 --> 00:07:11,680
And of course here, we wanna open up a server

141
00:07:11,680 --> 00:07:14,170
that has something to do with the network,

142
00:07:14,170 --> 00:07:16,280
and therefore it's blocked.

143
00:07:16,280 --> 00:07:19,900
So, to make sure that this code works,

144
00:07:19,900 --> 00:07:23,440
I now have to run app.ts,

145
00:07:23,440 --> 00:07:25,840
with an extra permission flag

146
00:07:25,840 --> 00:07:28,430
but here it's now, not allow write,

147
00:07:28,430 --> 00:07:31,700
because I'm not trying to write to any file

148
00:07:31,700 --> 00:07:35,330
instead, it's allow net and that's the permission flag

149
00:07:35,330 --> 00:07:37,870
for granting network permissions.

150
00:07:37,870 --> 00:07:40,500
And if I now hit enter, this executes the file

151
00:07:40,500 --> 00:07:43,310
with those proper permissions,

152
00:07:43,310 --> 00:07:47,140
and therefor now, we can open up localhost 3000

153
00:07:47,140 --> 00:07:51,270
in the browser, and we see "Hello world there"

154
00:07:51,270 --> 00:07:53,990
which is the response we're sending back.

155
00:07:53,990 --> 00:07:57,010
And this is now also a process that keeps on running

156
00:07:57,010 --> 00:07:59,620
just like a Node server does,

157
00:07:59,620 --> 00:08:01,690
until we quit it with Ctrl + C,

158
00:08:01,690 --> 00:08:05,250
because of course it keeps on listening for requests.

159
00:08:05,250 --> 00:08:07,460
We don't want this process to finish,

160
00:08:07,460 --> 00:08:09,080
it should keep on listening,

161
00:08:09,080 --> 00:08:11,720
and that's just what it does.

162
00:08:11,720 --> 00:08:15,850
Now, just as before, to have that side by side comparison,

163
00:08:15,850 --> 00:08:20,410
let's see how we would spin up a similar server, with Node.

164
00:08:20,410 --> 00:08:22,420
For that I'm going to quit it here,

165
00:08:22,420 --> 00:08:24,850
and now again, going to write some Node code.

166
00:08:24,850 --> 00:08:26,620
Feel free to do this on your own first,

167
00:08:26,620 --> 00:08:28,233
we'll do it together thereafter.

