1
00:00:02,250 --> 00:00:08,820
So we played around with Node.js a bit and we saw one of its built-in core modules which you can pull in to

2
00:00:08,820 --> 00:00:14,280
add certain functionalities which are not available in the browser but which are available in Node.js.

3
00:00:15,580 --> 00:00:20,800
One important difference compared to the browser of course is that some modules need to be imported

4
00:00:20,800 --> 00:00:28,180
with this syntax in order to use them whilst others are available globally and most modules actually

5
00:00:28,180 --> 00:00:30,520
need to be imported by the way, in the browser

6
00:00:30,520 --> 00:00:33,290
all the things by default are available globally,

7
00:00:33,310 --> 00:00:35,160
you can just access documents like this,

8
00:00:35,200 --> 00:00:37,330
you can just access local storage like this.

9
00:00:37,330 --> 00:00:39,630
That's one difference, in Node.js, 

10
00:00:39,630 --> 00:00:43,650
it's more of you have to tell me what you need approach, other than that

11
00:00:43,650 --> 00:00:45,240
however, it's highly comparable,

12
00:00:45,250 --> 00:00:49,270
we have the same base language and then some specific APIs.

13
00:00:49,390 --> 00:00:52,950
Now let's move away from this dummy script here,

14
00:00:53,140 --> 00:00:58,830
I'll name it files.js and add a new app.js which is empty and let's see what we can do in Node.js

15
00:00:58,840 --> 00:01:06,130
when it comes to working with HttpRequests because that arguably is an important scenario where you

16
00:01:06,130 --> 00:01:10,310
would look to Node.js running a web server with it.

17
00:01:10,360 --> 00:01:12,370
So let's see how that would work.

18
00:01:12,400 --> 00:01:14,520
It all starts with a specific module

19
00:01:14,530 --> 00:01:17,560
you can require and that's the HTTP module,

20
00:01:17,560 --> 00:01:22,390
it's a module that allows you to work with HttpRequests This by the way does not just mean that you

21
00:01:22,390 --> 00:01:23,840
can listen to them,

22
00:01:23,890 --> 00:01:29,530
you can also create a request and send it to a server but more commonly, you use Node.js to set up a

23
00:01:29,530 --> 00:01:34,720
server that listens to incoming requests so that you can have your own web server and in the end by

24
00:01:34,720 --> 00:01:36,100
the way, that's also what the webpack

25
00:01:36,100 --> 00:01:41,980
dev server and all these other development servers do, they use Node.js to set up a basic web server

26
00:01:42,010 --> 00:01:49,070
which runs locally on your system to host your HTML files, your simple local web application.

27
00:01:49,120 --> 00:01:54,730
Now we'll basically do the same here but we'll not just build our own development server but instead

28
00:01:54,730 --> 00:01:58,830
we'll build a server which also runs some server-side logic, something

29
00:01:58,840 --> 00:02:04,360
development servers of course don't do, they just serve our local files. Now to get started with that,

30
00:02:04,810 --> 00:02:09,070
we need to create a server and for that on the HTTP method,

31
00:02:09,070 --> 00:02:16,360
we can call create server. The create server method does just what it sounds like and it requires one important

32
00:02:16,360 --> 00:02:17,230
argument for that,

33
00:02:17,340 --> 00:02:19,220
a request listener,

34
00:02:19,390 --> 00:02:23,490
this is a function which in the end triggers for every incoming request.

35
00:02:23,620 --> 00:02:29,040
This request listener function takes two arguments which are passed in automatically by Node.js and

36
00:02:29,050 --> 00:02:32,230
that's a request object and a response object.

37
00:02:32,320 --> 00:02:38,050
So this will basically be passed in by Node.js and will reflect the request which was coming in, so

38
00:02:38,050 --> 00:02:43,540
the headers which are part of the request, the data which might be part of the request and so on and this

39
00:02:43,540 --> 00:02:49,330
response object is also given to you by Node.js, it's an object which you can then use to configure

40
00:02:49,510 --> 00:02:53,850
the response you want to send back and to then ultimately send it back.

41
00:02:54,040 --> 00:02:55,000
So inside here of

42
00:02:55,010 --> 00:03:00,130
create server, we can now work with these two pieces to read data from the incoming request and to send

43
00:03:00,130 --> 00:03:02,710
back a response and to start

44
00:03:02,710 --> 00:03:07,780
very simple, let's start with the response object to right now do nothing with the incoming request but

45
00:03:07,780 --> 00:03:15,850
to just send back a response and there we can call write, to write some data to the response,

46
00:03:16,040 --> 00:03:23,600
for example a simple string where we say hello there and then we can call end which will send the response,

47
00:03:23,630 --> 00:03:28,320
so end basically tells Node.js we're done adding data to the response,

48
00:03:28,340 --> 00:03:30,650
you can now close it and send it back

49
00:03:30,650 --> 00:03:33,720
and this is a very simple web server

50
00:03:33,730 --> 00:03:35,610
we could build with Node.js.

51
00:03:35,720 --> 00:03:41,480
Now with the server created, we need to do one more thing though, we need to call listen on it,

52
00:03:41,870 --> 00:03:47,390
so we either chain this here or we take the extra step of storing this in a separate constant and then

53
00:03:47,390 --> 00:03:53,630
we call listen on this constant, so on the object, on the server object stored in the constant. Listen is

54
00:03:53,630 --> 00:03:55,980
required to then actually start the server,

55
00:03:56,000 --> 00:04:02,240
you can think of create server as a method to build the server but then listen starts it up and listens

56
00:04:02,240 --> 00:04:03,750
to incoming requests.

57
00:04:03,920 --> 00:04:06,740
Listen also takes the port on which you want to listen

58
00:04:06,740 --> 00:04:13,040
and here I'll go for 3000 for my local development only to use this special port locally here.

59
00:04:13,170 --> 00:04:19,010
With that if we now save this, we can again run our app.js file which now holds the server and you

60
00:04:19,010 --> 00:04:22,960
will notice something interesting. If I do that, we don't get an error

61
00:04:23,150 --> 00:04:29,060
but we also don't complete this process, my cursor here in the terminal is stuck.

62
00:04:29,060 --> 00:04:35,150
Now you can always quit this by hitting control c but it is stuck for a good reason, it is stuck because

63
00:04:35,150 --> 00:04:36,980
this is an ongoing process,

64
00:04:37,010 --> 00:04:39,290
this script now doesn't finish

65
00:04:39,290 --> 00:04:46,160
once all the lines have been parsed, instead listen in the end sets up an ongoing event listener you could

66
00:04:46,160 --> 00:04:50,150
say behind the scenes which waits for incoming requests

67
00:04:50,150 --> 00:04:57,560
and as long as Node.js notices ongoing event listeners, it does not shut down script execution which

68
00:04:57,560 --> 00:05:03,110
is why this script execution is still going on, because we have ongoing event listeners and that's of

69
00:05:03,110 --> 00:05:04,030
course what you want,

70
00:05:04,070 --> 00:05:09,400
you want your server to keep on running and not to quit after it was configured, right?

71
00:05:09,410 --> 00:05:12,130
So now we get this ongoing server and to see it in action,

72
00:05:12,140 --> 00:05:18,320
all we have to do is we have to visit localhost:3000 and there, you should see hello

73
00:05:18,340 --> 00:05:24,380
there on this page. You see hello there there because that's of course the data we are sending

74
00:05:24,380 --> 00:05:26,030
back in the simple response.

75
00:05:26,270 --> 00:05:28,340
It's not HTML data or anything like that,

76
00:05:28,460 --> 00:05:31,970
IT's a simple text but we see it here on our own

77
00:05:31,970 --> 00:05:35,150
Node.js server and now we're back in the browser

78
00:05:35,150 --> 00:05:37,630
but this is now not browser side Javascript,

79
00:05:37,840 --> 00:05:43,940
in the end this is the result of server side Javascript which spins up a server and sends back a response.
