Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js has the largest ecosystem of open source libraries due to its package manager npm. The document discusses using Node.js because developers already know JavaScript, its performance advantages, and tools like npm. It also provides instructions on installing Node.js and the Node Version Manager (nvm), and explains common Node.js commands.
2. Node.js
Node.js® is a JavaScript runtime built on
Chrome's V8 JavaScript engine. Node.js
uses an event-driven, non-blocking I/O
model that makes it lightweight and
efficient. Node.js' package ecosystem,
npm, is the largest ecosystem of open
source libraries in the world.
4. Why Use Node.js
1. You Already Know JavaScript
2. It's Fast
1. Node.js is a JavaScript runtime that uses the V8 engine
developed by Google for use in Chrome.
3.Tooling (NPM)
4.Real-time Made Easy
5.One Codebase And Your Real time For Free
1. https://www.meteor.com/
5. Nodejs installation
Node Version Manager
The Node Version Manager allows admins to easily manage
node.js versions. It’s a bash script that has the capability to
manage multiple active versions of node.js, with
functionality such as: installation, executing commands with
specific node.js versions, setting the PATH variable to use a
specific node.js versions
curl -o-
https://raw.githubusercontent.com/creationix/nvm/v0.31.0/i
nstall.sh | bash
6. Let us know. What is the
meaning of these commands?
nvm install
nvm use
nvm current
nvm ls
nvm alias default
7. JavaScript & Node.js
Only in Javascript
window
location
document
Only in Node.js
global
process
module
9. Create Server
var http = require('http');
var handleRequest = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to ITI Intake36 n');
};
var server = http.createServer(handleRequest);
server.listen(3000, 'localhost');
11. Create Server
var http = require('http');
To require modules
What is The difference between
var http = require('http');
AND
var http = require('./http');
16. Callback
Node.js is a JavaScript runtime built on
Chrome's V8 JavaScript engine. Node.js
uses an event-driven, non-blocking I/O
mode
Callback is an asynchronous equivalent
for a function. A callback function is
called at the completion of a given task.
17. Callback
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
if (err) return console.error(err);
console.log(data.toString());
});
console.log("Program Ended");
18. callback
var fs = require("fs");
var data =
fs.readFileSync('input.txt');
console.log(data.toString());
console.log("Program Ended");
20. In Node.js, there are four types of
streams
Readable - Stream which is used for read operation.
Writable - Stream which is used for write operation.
Duplex - Stream which can be used for both read
and write operation.
Transform - A type of duplex stream where the
output is computed based on input.
21. var fs = require("fs");
var data = '';
var readerStream = fs.createReadStream('input.txt');
readerStream.setEncoding('UTF8');
readerStream.on('data', function(chunk) {
data += chunk;
});
readerStream.on('end',function(){
console.log(data);
});
readerStream.on('error', function(err){
console.log(err.stack);
});
console.log("Program Ended");
24. var http = require('http');
var fs = require('fs');
var url = require('url');
http.createServer( function (req, res) {
var pathname = url.parse(req.url).pathname;
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
res.writeHead(404, {'Content-Type': 'text/html'});
}else{
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data.toString());
}
res.end();
});
}).listen(3000);
25. CONNECT TO DATA BASE
we will used mysql data base
and we will use mysql module
https://www.npmjs.com/package/mysql
26. CONNECT TO DATABASE
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'iti'
});
connection.query('SELECT * from books', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
27. Web service
A web service is any piece of
software that makes itself
available over the internet
28. REST
Representational State Transfer (REST) is
a software architecture style consisting of
guidelines and best practices for creating
scalable web services.