This video tutorial illustrates searching, installing, updating and removing the external, free modules.
External Free Node Modules are listed at npmjs.org and is managed by NPM – Node Package Manager.
There are so many amazing modules already written to handle so many sophisticated functionalities, that it becomes easy to use them in our application and build a robust, scalable application quickly.
Command to look at all the external module names and their respective description
1
npm search
npm search
It gives a long list of names and descriptions.
To make is look better, you can use GREP(if you’re on Linux or Unix based System) Search with GREP
1
npm search | grep module_name
npm search | grep module_name
npm search followed by pipe symbol, which is followed by any module name.
For Windows users
1
npm search module_name
npm search module_name
it gives all the details of available methods, properties and their description.
Installation of Modules
init command
1
npm init
npm init
Once you run this command, it prompts you with some optional things, like, name of the project, version, author, licensing etc. Once you’re done with that, it creates package.json file with all the details you provided.
The ‘init’ command in NPM allows us to identify our project and list out any Node Modules it requires.
Now open that package.json file: it looks somewhat like this package.json file
You need to open this package.json file using a plain text editor and add your project dependencies i.e., some of the modules you need to install for your project.
In this video tutorial we’re installing 3 external modules: express jade mongoose
What is a module? A module is a self-contained series of one or more javascript files represented by an object.
Update Command
1
npm update
npm update
once you run this command, package.json file is parsed and the dependency modules are checked for any updates, if any, it’ll be updated. If any module is installed with -g (global scope), i.e., the root installation, then while updating or removing them, it may throw some errors, in such case, use sudo keyword:
Update Command
1
sudo npm update -g
sudo npm update -g
This would solve the problem both for updating and deleting external modules.
remove/delete module command
1
npm prune
npm prune
To remove the unused modules: open package.json file and remove the module names which you want to remove. Save the package.json file and then run above(npm prune) command. This would safely remove the modules you no more intend to use in your project.
List Global module: using command
1
npm -g ls
npm -g ls
This lists all the global modules, it’s path and file structure: as shown below:
exports is a global provided by node.js students is a name given by us; it’s a property name and we assign an array to it which contains some names.
Application File appStu.js
1
2
3
var students = require('./students');
console.log(students.students);
var students = require('./students');
console.log(students.students);
We require the local module students in appStu.js file. ./ (dot followed by a forward slash) specifies that it’s a local module and it need not search for the file in core module or node module collection.
Next, object.property name will print out the entire array present inside the local module file.
In this video tutorial we shall see how to gather user input and process it inside our node.js application.
In this application, we’ll be asking the user to enter his/her work experience and based on the user input we’ll do some logical processing and output appropriate result to the user.
readline Module readline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var rl = require('readline');
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("No of years you've been working in corporate?",
function(experience){
var msg = "";
if( experience < 5 )
msg = "You're short of "+(5-experience)+" years experience to
attend job interview at Apple Inc!";
else
msg = "Excellent, you're eligible for the job interview!!";
console.log(msg);
process.exit();
});
var rl = require('readline');
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("No of years you've been working in corporate?", function(experience){ var msg = ""; if( experience < 5 ) msg = "You're short of "+(5-experience)+" years experience to attend job interview at Apple Inc!"; else msg = "Excellent, you're eligible for the job interview!!"; console.log(msg); process.exit();
});
readline module is built into node.js to facilitate user input in node applications.
createInterface method is used to create an interface for user input. createInterface method takes 2 parameters: process.stdin and process.stdout. These are standard input and standard output properties of the current process.
Using the createInterface object, we invoke question method.
question method takes 2 parameters: First one being the question(something which is to be prompted to the user). Second parameter is an anonymous function.
Response of the user is passed in as argument to this anonymous function.
Inside this anonymous function, we use some simple conditional logic and display the appropriate message to the user, based on his/her input.
i.e., if the person has less than 5 years of job experience, then he/she is not eligible to the job interview at Apple inc. If the job experience is greater than 5 years, then he/she is eligible!!
Finally, we display the content of msg variable using console.log and exit from the process.
process.exit() is very important. If we don’t exit the process, the interface keeps reading from the standard input.
Output node readline.js No of years you’ve been working in corporate? 3 You’re short of 2 years experience to attend job interview at Apple Inc!
node readline.js No of years you’ve been working in corporate? 9 Excellent, you’re eligible for the job interview!!
Note: readline module is built into node.js because this is used most often in node applications. User input is a crucial thing in any sophisticated application.
var http = require('http');
var visits = 0;
http.createServer( function(request, response) {
console.log('New Connection');
visits++;
response.writeHead(200, { 'Content-Type':'text/plain' } );
response.write('Hello\n');
response.write('We have had '+visits+' visits');
response.end();
}).listen(8080);
console.log('Server Started..');
var http = require('http'); var visits = 0; http.createServer( function(request, response) {
console.log('New Connection');
visits++;
response.writeHead(200, { 'Content-Type':'text/plain' } );
response.write('Hello\n');
response.write('We have had '+visits+' visits');
response.end(); }).listen(8080); console.log('Server Started..');
Here we take a variable called visits and initialize it to 0. Once there is a new connection request, we increment it’s value by 1. Since there is also request from favicon, practically, there’ll be 2 requests simultaneously: hence the counter increments by 2 for each new connection request.
using write method, we print the number of visits.