Friday, 3 March 2017

Node js tutorial for beginners with examples

node js tutorial for beginners with examples

Question: What is REPL?
It represents a computer environment like a Unix/Linux shell where a command is entered and the system responds with an output.


Question: What is full form of REPL?
Read. Eval. Print. Loop.


Question: How to start REPL?
Type following command and press enter key.
node



Question: How to calculate 2+2 in Node REPL?
Just type 2+2 and press enter key in REPL Mode.


Question: How to see previous result in REPL?
Use Underscore to see the previous results.
_



Question: What we can do in REPL?
  1. Simple Expression
  2. Use Variables
  3. Multiline Expression
  4. Underscore Variable



Question: What are most common REPL Commands?
ctrl + c ? terminate the current command.
ctrl + c twice ? terminate the Node REPL.
ctrl + d ? terminate the REPL.
Up/Down Keys ? see command history.
tab Keys ? list of current commands.
.help ? list of all commands.
.break ? exit from multiline expression.
.clear ? exit from multiline expression.
.save filename ? save the current session to a file.
.load filename ? load file content in Node REPL session.


Question: What is the difference between local and global module in Node.js?
When we Installed a module locally, then we need to use require() to use module.
When we Installed a module globally, then we need't to use require() to use module.


Question: When to use local and global module?
If we are going to run it on the command line, then we must install the module globally.


Question: How to install mysql locally?
npm install mysql



Question: How to install mysql globally?
npm install mysql -g



Question: What is package.json in module?
All npm packages contain a file, usually in the project root, called package.json.
package.json file holds various metadata relevant to the project.
This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.
It can also contain other metadata like description, version, license and configuration data.


Question: Give the sample of package.json?
{
  "name" : "name of project",
  "description" : "Project description.",
  "homepage" : "http://example.com",
  "keywords" : ["keyword1", "keyword2", "keyword3", "keyword4", "keyword5"],
  "author" : "hellouser ",
  "contributors" : [],
  "dependencies" : [],
  "repository" : {"type": "git", "url": "git://github.com/example/example.git"},
  "main" : "project.js",
  "version" : "1.2.7"
}


Question: Does package.json exist for each module?
Yes, each package contain the package.json.
It exist in root of module.


Question: Does package.json exist for each module?
Yes,
Each module have its own package.json


Question: How to search a new module?
npm search mysql



Question: How to update existing module?
npm update mysql



Question: Can we create a new module and published?
Yes, We can create a new module.
Following are different ways.
  1. Create package.json
  2. Create a new module using "npm init" commond.
  3. Add the user for new module.
  4. Publish the node module using "npm publish"



Question: Give example Blocking vs non-blocking in node.js?
Create a file with "input.txt" with content "Hello I am here.";


Blocking code
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("End of script");

Output
Hello I am here.
End of script

Non-Blocking code
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
   if (err) return console.error(err);
   console.log(data.toString());
});
console.log("End of script");
Output
End of script
Hello I am here.


Question: How to install express-session ?
In Browser (client)
npm install express-session



Question: How to include express-session?
In Server(Node)
var session = require('express-session');
app.use(session({secret: 'this is secret key'}));




No comments :

Post a Comment