Presentation is loading. Please wait.

Presentation is loading. Please wait.

Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.

Similar presentations


Presentation on theme: "Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager."— Presentation transcript:

1 Node.JS introduction

2 What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager and module system

3 JavaScript everywhere Code reuse Same programming culture on client and server Lots of JavaScript programmers

4 Node.JS standrard libraries Async by default Low level HTTP is first class citizen

5 NPM node’s package manager handles and resolves dependencies about 50,000 modules available

6 Routes with Express Respond to HTTP requests with a callback Supports variable placement in routes Easy to serve JSON Example app.get("/greeting", function(req,res) { res.send("Hello, "+req.query.name+"!"); });

7

8

9

10

11 Task 2

12 // module.js exports.name = function() { console.log('My name is John'); }; var module= require('./module.js'); module.name(); // Modules in NodeJS

13 Using sessions in NodeJS app.use(session({ secret: 'angular_tutorial', resave: true, saveUninitialized: true, })); app.get("/notes", function(req,res) { res.send(req.session.notes||[]); }); app.post("/notes", function(req, res) { if (!req.session.notes) { req.session.notes = []; req.session.last_note_id = 0; } var note = req.body; note.id = req.session.last_note_id; req.session.last_note_id++; req.session.notes.push(note); res.end(); });

14 https://www.npmjs.com

15 Popular NPM modules

16 sudo npm install -g forever Use forever -w, instead of node to start your app: $ forever -w app.js Installation: Forever

17 Task 4

18 Reading files var fs = require("fs"); //Asynchronous read fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("Asynchronous read: " + data.toString()); }); //Synchronous read var data = fs.readFileSync('input.txt'); console.log("Synchronous read: " + data.toString()); console.log("Program Ended");

19 Writing files var fs = require("fs"); console.log("Going to write into existing file"); fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) { if (err) { return console.error(err); } console.log("Data written successfully!"); console.log("Let's read newly written data"); fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("Asynchronous read: " + data.toString()); });

20 Task 6


Download ppt "Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager."

Similar presentations


Ads by Google