1
00:00:00,000 --> 00:00:04,614
[MUSIC]

2
00:00:04,614 --> 00:00:10,618
Now that we have an understanding of node
modules, let's do our first exercise

3
00:00:10,618 --> 00:00:17,080
to further our understanding of node
modules with an example in this exercise.

4
00:00:17,080 --> 00:00:22,730
So to get started, let's go to
a convenient location on our computer.

5
00:00:22,730 --> 00:00:25,600
So here,
I am in my documents Coursera folder,

6
00:00:25,600 --> 00:00:30,740
where I have the files from
all the previous courses.

7
00:00:30,740 --> 00:00:35,680
So I'm going to create a new folder here,
named NodeJS and

8
00:00:35,680 --> 00:00:42,020
this folder is where I'm going to store
all the code related to the NodeJS course.

9
00:00:42,020 --> 00:00:44,370
So going into the NodeJS folder,

10
00:00:44,370 --> 00:00:48,845
let's create a subfolder here,
named node-examples.

11
00:00:53,410 --> 00:00:54,590
And in this folder,

12
00:00:54,590 --> 00:01:00,470
we're going to be creating
the files related to this exercise.

13
00:01:01,880 --> 00:01:07,018
Open a terminal window or
a command window, and

14
00:01:07,018 --> 00:01:13,442
then navigate to the node-examples
folder at the prompt,

15
00:01:13,442 --> 00:01:17,681
and then at the prompt, type npm init to

16
00:01:17,681 --> 00:01:22,837
initialize this folder
as a node application.

17
00:01:22,837 --> 00:01:27,757
So type npm init and
then it will come up with some

18
00:01:27,757 --> 00:01:33,050
standard default values for
some information that

19
00:01:33,050 --> 00:01:38,120
is going to store in
a file named package.json.

20
00:01:38,120 --> 00:01:45,251
So for the name, let's type node-examples,

21
00:01:45,251 --> 00:01:52,383
and version is 1.1.0, and description,

22
00:01:52,383 --> 00:01:58,639
I'll simply type Simple Node Examples.

23
00:02:00,565 --> 00:02:05,200
And the entry point is
index.js default value.

24
00:02:05,200 --> 00:02:09,210
For the test command just leave it as such
and the Git repository for the moment,

25
00:02:09,210 --> 00:02:10,890
we'll leave it as such.

26
00:02:10,890 --> 00:02:14,790
And for the author,
just fill in your name.

27
00:02:17,430 --> 00:02:22,610
And the rest, and then npm init will show

28
00:02:22,610 --> 00:02:27,510
the default value that it'll set
up in the package.json file.

29
00:02:27,510 --> 00:02:31,960
So as we have learned, the package.json
file is the manifest file for

30
00:02:31,960 --> 00:02:36,210
a node application, and once you
complete this, you will notice that

31
00:02:36,210 --> 00:02:41,500
the package.json file is created
in that node-examples folder.

32
00:02:41,500 --> 00:02:46,980
Let's open this project in
an editor of our choice.

33
00:02:46,980 --> 00:02:53,350
So I'm going to open my
Visual Studio code at this folder.

34
00:02:53,350 --> 00:02:58,600
When my Visual Studio code,
it opens in that folder.

35
00:02:58,600 --> 00:03:05,100
I'm going to go to the scripts here and
set up a script here saying "start"

36
00:03:06,500 --> 00:03:10,599
and I'll say "node index".

37
00:03:11,830 --> 00:03:16,460
And then save the changes
to the package.json file.

38
00:03:16,460 --> 00:03:22,595
Next I'm going to create a folder here and
name it as index.js and

39
00:03:22,595 --> 00:03:28,267
as you realized that main here
is given as index.js file,

40
00:03:28,267 --> 00:03:33,470
so that starting file for
my node application.

41
00:03:33,470 --> 00:03:38,600
So in the index.js file,
I'm going to create a simple example

42
00:03:38,600 --> 00:03:43,760
to illustrate various features of node and
how we will construct node modules.

43
00:03:43,760 --> 00:03:49,410
So here, we'll construct an example
that evaluate the area and

44
00:03:49,410 --> 00:03:51,960
the perimeter of a rectangle.

45
00:03:51,960 --> 00:03:56,280
A very simple example that
it helps us to illustrate

46
00:03:56,280 --> 00:03:59,480
various aspects about node and
node modules.

47
00:03:59,480 --> 00:04:05,336
So the get started type, Var rect.

48
00:04:05,336 --> 00:04:10,787
And we're going to create
a JavaScript object here,

49
00:04:10,787 --> 00:04:17,930
that's two properties,
perimeter, which is a function.

50
00:04:17,930 --> 00:04:26,040
And as you see, I am using the arrow
function to define this function here.

51
00:04:26,040 --> 00:04:32,100
So node in its current version
supports ES 6 or ES 2015.

52
00:04:32,100 --> 00:04:36,251
And so that is why I'm going to
use the arrow function support

53
00:04:36,251 --> 00:04:41,210
that is available in ES 2015
to define this function here.

54
00:04:41,210 --> 00:04:46,720
And if you have taken the previous
courses where we use time skirt,

55
00:04:46,720 --> 00:04:48,800
this would be quite familiar to you.

56
00:04:50,340 --> 00:04:57,560
Now with node, we have the option
of using JavaScript or Typescript.

57
00:04:57,560 --> 00:05:02,350
Now the documentation for node and

58
00:05:02,350 --> 00:05:07,865
also express that we will see later,
all use standard JavaScript.

59
00:05:07,865 --> 00:05:13,465
So I have defaulted to using standard
JavaScript in this course or

60
00:05:13,465 --> 00:05:16,505
ES2015 JavaScript in this course.

61
00:05:16,505 --> 00:05:18,585
If you prefer to use TypeScript,

62
00:05:18,585 --> 00:05:24,620
you can set up your node examples to
write your application in TypeScript.

63
00:05:24,620 --> 00:05:28,740
But then you will have to transpile
that code into JavaScript so

64
00:05:28,740 --> 00:05:31,090
that it can be run using Node.

65
00:05:31,090 --> 00:05:34,150
So you have to set up
additional infrastructure

66
00:05:34,150 --> 00:05:36,640
in order to be able to use TypeScript.

67
00:05:36,640 --> 00:05:40,630
In this course, I am going to use
JavaScript as the default language for

68
00:05:40,630 --> 00:05:41,960
the rest of the course.

69
00:05:41,960 --> 00:05:46,790
And I'm not going to go into
Typescript use it in this course

70
00:05:46,790 --> 00:05:51,210
when nodes start supporting
Typescript natively,

71
00:05:51,210 --> 00:05:55,220
then it makes more sense to
move to Typescript [INAUDIBLE].

72
00:05:55,220 --> 00:05:57,550
But if you are interested in Typescript,

73
00:05:57,550 --> 00:06:01,770
I have noticed that there have been
several blog articles posted by different

74
00:06:01,770 --> 00:06:07,400
people that illustrate how you can set up
your Node application to use Typescript.

75
00:06:07,400 --> 00:06:11,420
Having said that,
let's return to the index.js file and

76
00:06:11,420 --> 00:06:13,030
then continue with our example.

77
00:06:13,030 --> 00:06:20,340
So I'm setting up a perimeter here and
then also an area function here.

78
00:06:20,340 --> 00:06:22,460
Again, taking two values, x and y,

79
00:06:22,460 --> 00:06:26,650
which represent the length and
the breadth of the rectangle.

80
00:06:26,650 --> 00:06:31,830
The two parameters, and
the area is computed as x times y.

81
00:06:31,830 --> 00:06:36,150
So from your primary school mathematics,

82
00:06:36,150 --> 00:06:40,670
you know how to compute the perimeter and
the area of the rectangle.

83
00:06:40,670 --> 00:06:45,120
So here, I have set up this
JavaScript object here and

84
00:06:45,120 --> 00:06:50,690
then let me define a function
here named solveRect,

85
00:06:50,690 --> 00:06:54,166
which takes two values, (l,

86
00:06:54,166 --> 00:06:58,180
b), as the two parameters, the length and
the berth of the rectangle.

87
00:06:58,180 --> 00:07:03,409
And it is going to use this
JavaScript object that I have defined

88
00:07:03,409 --> 00:07:09,750
earlier in order to compute the perimeter
and the area of he rectangle.

89
00:07:09,750 --> 00:07:12,762
So inside here,

90
00:07:12,762 --> 00:07:17,710
let me do a console.log and

91
00:07:17,710 --> 00:07:25,035
say Solving for rectangle with l equal to

92
00:07:28,483 --> 00:07:30,827
This is just for our convenience.

93
00:07:40,010 --> 00:07:45,335
So that will print out this information
onto the terminal there and

94
00:07:45,335 --> 00:07:50,944
so in this console.log, I'm just
printing out the l and b values, so

95
00:07:50,944 --> 00:07:58,130
that I know for the rectangle for which we
are computing the length and the breadth.

96
00:07:58,130 --> 00:08:03,118
Now, I'm going to check to
make sure the length and

97
00:08:03,118 --> 00:08:07,870
the breadth are not less than or
equal to zero.

98
00:08:07,870 --> 00:08:13,340
If they are less than or equal to zero,
then it doesn't make sense to compute

99
00:08:13,340 --> 00:08:19,790
the Perimeter and area for the rectangle.

100
00:08:19,790 --> 00:08:27,957
So I will say console.log here,

101
00:08:27,957 --> 00:08:33,790
so I would say Rectangle

102
00:08:33,790 --> 00:08:39,040
dimensions should be

103
00:08:39,040 --> 00:08:44,008
greater than zero.

104
00:08:58,175 --> 00:09:04,030
So that way, we print out these values
on to the screen if the length and

105
00:09:04,030 --> 00:09:07,640
the breadth are less than or
equal to zero.

106
00:09:07,640 --> 00:09:11,533
Otherwise of course, it makes
sense to compute the perimeter and

107
00:09:11,533 --> 00:09:13,370
the area of the rectangle.

108
00:09:13,370 --> 00:09:16,944
So we'll say else

109
00:09:16,944 --> 00:09:22,428
console.log("The area

110
00:09:22,428 --> 00:09:27,679
of the rectangle is").

111
00:09:55,345 --> 00:09:57,416
Similarly, we'll compute,

112
00:10:10,161 --> 00:10:12,217
The perimeter of the rectangle.

113
00:10:21,787 --> 00:10:22,562
Here.

114
00:10:24,130 --> 00:10:27,602
So inside this function,
we are computing the area and

115
00:10:27,602 --> 00:10:31,210
the perimeter of the rectangle and
purging it out.

116
00:10:31,210 --> 00:10:36,058
Now, we obviously need to call this
function using different values for

117
00:10:36,058 --> 00:10:39,230
length and breadth and
see what it prints up.

118
00:10:39,230 --> 00:10:45,196
So right below here,
I'm going to say solveRect and

119
00:10:45,196 --> 00:10:48,150
then just use some values here.

120
00:10:48,150 --> 00:10:54,533
So I'll say 2, 4, And 3, 5.

121
00:10:58,375 --> 00:11:04,771
And let's say 0, 5 and see what happens.

122
00:11:04,771 --> 00:11:11,957
And -3, 5.

123
00:11:11,957 --> 00:11:16,099
So four different choices
of parameters here to

124
00:11:16,099 --> 00:11:21,130
illustrate how this
application is going to work.

125
00:11:21,130 --> 00:11:24,640
So with this, let's save the changes.

126
00:11:24,640 --> 00:11:30,590
Now going back to the terminal,
at the prompt, type npm start and

127
00:11:30,590 --> 00:11:36,530
you would see that it prints out
the area of the perimeter for

128
00:11:36,530 --> 00:11:39,980
the different rectangles that
we have specified there.

129
00:11:39,980 --> 00:11:44,000
So we are saying solving for
the rectangle with l is equal to two and

130
00:11:44,000 --> 00:11:48,890
b is equal to four, and the area of
the perimeter for this rectangle.

131
00:11:48,890 --> 00:11:53,190
And for the second one, and for
the third one, as you see, your

132
00:11:54,380 --> 00:11:58,870
dimensions should be greater than zero and
for

133
00:11:58,870 --> 00:12:02,450
the fourth one also,
it is printing out the same value.

134
00:12:02,450 --> 00:12:07,010
Now obviously, I notice that I would
need a space there, so I'll just correct

135
00:12:07,010 --> 00:12:12,731
the code to add in that extra space there.

136
00:12:12,731 --> 00:12:18,540
So with this, we have seen how we
can write a simple node application

137
00:12:18,540 --> 00:12:24,260
to compute the perimeter and
the area of a rectangle.

138
00:12:24,260 --> 00:12:27,780
Now obviously, you want to be
able to set up a Git repository,

139
00:12:27,780 --> 00:12:31,930
where you can store all this code.

140
00:12:31,930 --> 00:12:39,110
So at the prompt type git init to
initialize the Git repository and

141
00:12:39,110 --> 00:12:45,195
then you can check the status
of the Git repository and

142
00:12:45,195 --> 00:12:48,655
you notice that two files
have been newly created.

143
00:12:48,655 --> 00:12:52,651
So let's add the two files
to our Git repository, and

144
00:12:52,651 --> 00:12:56,535
at this point,

145
00:12:56,535 --> 00:13:00,505
checking the git status, you see that
the two files have been checked in.

146
00:13:00,505 --> 00:13:06,611
So let's do a git commit break

147
00:13:06,611 --> 00:13:13,710
the message Simple Node Example.

148
00:13:13,710 --> 00:13:18,970
In the second part of this exercise,
we're going to now

149
00:13:20,130 --> 00:13:24,330
create a node module in this example.

150
00:13:24,330 --> 00:13:29,191
So at the editor, in the editor,

151
00:13:29,191 --> 00:13:34,610
create a file named rectangle.js.

152
00:13:34,610 --> 00:13:38,130
And inside this file,
we're going to create a node module.

153
00:13:38,130 --> 00:13:43,100
So we're going to use the exports

154
00:13:43,100 --> 00:13:47,892
object in order to export the two

155
00:13:47,892 --> 00:13:52,517
values from this node module.

156
00:13:52,517 --> 00:13:59,063
So as you've learned, every file in a node
application becomes its own node module.

157
00:13:59,063 --> 00:14:02,264
So from within this rectangle node module,

158
00:14:02,264 --> 00:14:05,909
I'm going to export two values,
the perimeter,

159
00:14:09,841 --> 00:14:15,544
And The area.

160
00:14:15,544 --> 00:14:22,080
Now I"m going to go into index.js file and
then simply copy this value.

161
00:14:24,610 --> 00:14:29,558
And that is what we will
use as the export for

162
00:14:29,558 --> 00:14:33,820
the perimeter here and similarly for

163
00:14:33,820 --> 00:14:38,495
the area I'm just going to copy this from

164
00:14:38,495 --> 00:14:44,340
the index js file and
then use that for the area.

165
00:14:44,340 --> 00:14:49,498
So now, we have a simple node module
ready, which is exporting two values,

166
00:14:49,498 --> 00:14:53,222
the perimeter and
area from this rectangle.js file.

167
00:14:53,222 --> 00:14:57,583
So as we have learned in the lecture,
[INAUDIBLE] node modules,

168
00:14:57,583 --> 00:15:02,273
now once you create a node module,
you need to input that node module

169
00:15:02,273 --> 00:15:05,970
into another file where you
want to make use of it.

170
00:15:05,970 --> 00:15:09,771
So going back to index.js file.

171
00:15:09,771 --> 00:15:17,372
Now I'm going to replace this
whole thing by require and

172
00:15:17,372 --> 00:15:22,870
I note that this node
module is in the same

173
00:15:22,870 --> 00:15:27,250
folder as the current project.

174
00:15:27,250 --> 00:15:31,560
So I would say require rectangle and
that's it.

175
00:15:31,560 --> 00:15:33,900
So my changes are now complete.

176
00:15:33,900 --> 00:15:39,550
So this particular index.js file
is going to import the rectangle

177
00:15:39,550 --> 00:15:45,190
node module that we have just created, and
then make use of it within our code here.

178
00:15:45,190 --> 00:15:46,750
So let's save the changes and

179
00:15:46,750 --> 00:15:51,780
then take a look at our application again,
at the terminal prompt.

180
00:15:51,780 --> 00:15:56,160
Going to the terminal,
at the prompt, type npm start.

181
00:15:58,200 --> 00:16:02,100
You will notice that there modified

182
00:16:02,100 --> 00:16:07,310
version of this application produces
exactly the same results as before.

183
00:16:07,310 --> 00:16:12,229
With this, we learn about how
you can create a simple node

184
00:16:12,229 --> 00:16:14,699
module in our application.

185
00:16:14,699 --> 00:16:19,781
Now checking the git status,
you'll see that you have two files,

186
00:16:19,781 --> 00:16:25,370
one which has been modified,
another one which has just been added.

187
00:16:25,370 --> 00:16:30,050
So let's add this to
our Git repository and

188
00:16:30,050 --> 00:16:34,088
then do a git commit with the message,

189
00:16:34,088 --> 00:16:40,360
Simple Node Module.

190
00:16:42,160 --> 00:16:47,823
And Check the git log and

191
00:16:47,823 --> 00:16:52,455
you'll notice that we
have two commits that we

192
00:16:52,455 --> 00:16:56,987
have committed to our
Git repository there.

193
00:16:56,987 --> 00:16:59,468
With this, we complete this exercise.

194
00:16:59,468 --> 00:17:03,000
In this exercise, we have seen how we
can write a simple node application.

195
00:17:03,000 --> 00:17:06,612
And also how we can write
a simple node module and

196
00:17:06,612 --> 00:17:10,419
use it with another file
in our node application.

197
00:17:10,419 --> 00:17:16,469
[MUSIC]