ݺߣ

ݺߣShare a Scribd company logo
Node.js
BESHOY SEMSEM
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.
Node36
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/
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
Let us know. What is the
meaning of these commands?
 nvm install
 nvm use
 nvm current
 nvm ls
 nvm alias default
JavaScript & Node.js
Only in Javascript
window
location
document
Only in Node.js
global
process
module
Node36
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');
Node36
Create Server
var http = require('http');
To require modules
What is The difference between
var http = require('http');
AND
var http = require('./http');
Create Server
 function (req, res)
Res.writeHead
Res.writeHead
Res.end
Server.listen
Exports
exports vs module.exports
Node36
NPM
 npm install
 npm init
 npm publish
 npm login
 npm adduser
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.
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");
callback
var fs = require("fs");
var data =
fs.readFileSync('input.txt');
console.log(data.toString());
console.log("Program Ended");
Streams
Streams are objects
that let you read
data from a source
or write data to a
destination in
continuous fashion.
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.
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");
Web Application
Architecture
Create own web server
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);
CONNECT TO DATA BASE
we will used mysql data base
and we will use mysql module
https://www.npmjs.com/package/mysql
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();
Web service
A web service is any piece of
software that makes itself
available over the internet
REST
Representational State Transfer (REST) is
a software architecture style consisting of
guidelines and best practices for creating
scalable web services.
RESTful API HTTP methods
GET
POST
DELETE
PUT
Create web service
request
response
logic
format
Thank you :)
create server can handle crud
operation

More Related Content

Node36

  • 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');
  • 12. Create Server  function (req, res) Res.writeHead Res.writeHead Res.end Server.listen
  • 15. NPM  npm install  npm init  npm publish  npm login  npm adduser
  • 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");
  • 19. Streams Streams are objects that let you read data from a source or write data to a destination in continuous fashion.
  • 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");
  • 23. Create own web server
  • 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.
  • 29. RESTful API HTTP methods GET POST DELETE PUT
  • 31. Thank you :) create server can handle crud operation