Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
The Node community provide thousands of modules beyond what's included in Node core. In this video, we'll take a look at how to find those modules and then add them to a project. All of the community provided modules are available on npmjs.org. Npmjs.org acts as a directory for all the modules. I'm going to search for a popular module. The name of this module is express. When I search for express, the express module comes up as well several modules that work with express.
When I go to the page for express, it shows how many times its been downloaded, what version its at and how recently it was updated. The information on this page can give you a good idea of whether or not a module is actually being maintained. Or whether someone's just left it there. This module was last updated two days ago, so it's still active. Let's start using it in our project. To do that, let's go to the exercise files>chapter three>video two. And then go to the start folder and copy airline to the desktop.
Now let's go to the command line. Open up Terminal, type in cd space, and then drag the airline folder into Terminal to get the path, then press Return. Inside of this folder is a package.json file. This file is going to keep track of the modules that we install for the project as we install them. So let's install the express module now. Type in npm install. And then --save, and then express. The --save part is important.
That is going to record the version of express that we're installing, as we install it. If you don't add --save, it will simply install the module but it won't record the version. So that's now installed the express module. Now if we list the directory contents, you'll notice there's a node modules folder and package.json is still there. Let's take a look at package.json. Open it up in sublime text. On line 11, there's now a dependency section. This section is an object, and each object property is the name of a module.
In this case, it's recorded that we installed express 3.4.0. New lets use the express module. To do that, I'm going to create a file called index.js. I'm typing in the touch command to make a new blank file. And now with that file in place, I'm going to open it in sublime text. So first ,lets include both the http module and the express module. Now lets use express to create a new application. Express is going to allow us to define routes on this application. I'm going to type app.get for a get request, and then slash.
Then after that I'm going to supply a call-back function. This call-back function accepts two arguments. One is a, the request object, and one is the response object. I'm going to handle these requests by outputting some text. I'm calling the end method of the response object. Finally let's start up this server. First I'm calling the create server method of the http object, and then I'm calling the listen method. Then I'm telling it which port to listen on. Finally, let's run this code. Type in node index.js. And now let's go to the web browser, and go to local host at port 3000.
And it's showing our message. Once we find the right modules for our project, we can install them with npm install. By adding the save flag, they're automatically listed, in package.json. In the next video, we'll have a look at some of the other commands available through npm.
There are currently no FAQs about Node.js Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.