SlideShare a Scribd company logo
API Driven Applications
AngularJS, NodeJS and MongoDB
@HmidiHamdi
Software Engeneering | ISSATSo
Member & Founder | ISSATSo Google Club
CXO | OMNIA
Agenda
- API Driven Application
- Basics Of Node JS
- Discovering MongoDB
API DRIVEN APPLICATION
REST API
REST = Representation State Transfer.
- Client Sends HTTP verbs(GET, POST,
DELETE, PUT) along with a URL and
variable parameters that are unlencoded.
- The URL tells us what object to act on.
- Server Replies with a result code and valid
JSON.
HTTP Verbs - CRUD
- GET : when a client want to read an object.
- POST : when a client want to ceate an
object.
- PUT : when a client want to update an
object.
- DELETE : when a client want to delete an
object.
Why REST API ?
a simple way to create a data service enabling
you to easy create all your other applications:
- HTML5 / JAVASCRIPT.
- ANDROID.
- IOS.
MEAN STACK
M : MongoDB (Most Popular NoSql DataBase).
E : ExpressJS (Web Application Framework).
A : AngularJS (A Robust Framework for
creating HTML5 and Javascript rich web
Applications).
N : NodeJS (Server-side Javascript interpreter).
Getting started with node JS
What is NodeJS ?
● Open Source, Cross-platform runtime
environment for server-side and networking
applications.
● NodeJS provides an Event-Driven
architecture and non-blocking I/O API.
● Run your Javascript on Server.
● Uses V8 Engine.
Getting Started with Nodejs
- Install / Build Nodejs
- Type your Script
- open Terminal and run script using :
“node filename.js”
What can you do with NodeJS
- You can create an HTTP server and print ‘hello world’ on the browser in
just 4 lines of JavaScript.
- You can create a DNS server.
- You can create a Static File Server.
- You can create a Web Chat Application like GTalk in the browser.
- Node.js can also be used for creating online games, collaboration tools or
anything which sends updates to the user in real-time.
Hello Node JS
//On inclus la librairie http qui permet la création d'un serveur http basique
var http = require('http');
http.createServer( function (request, response) {
//Le type de contenu sera HTML, le code de réponse 200 (page correctement chargée)
response.writeHead(200, {'Content-Type': 'text/html'});
response.end('Hello <a href="http://www.google.com.fr”>World</a> :pn');
} ).listen(666);
//On affiche un petit message dans la console histoire d'être sûr que le serveur est lancé
console.log('Visit http://localhost:666 ');
Node Package Manager
Express JS
Uses
app.use(express.static(__dirname + '/Public'));
// set the static files location /public/img will be /img for users
app.use(morgan('dev'));
// log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.json());
// parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
// parse application/vnd.api+json as json
app.use(methodOverride());
Routes
app.get('/todos/:id', function (req, res, next) {
var todo=getTodo(id);
res.json(todo);
});
-------------
app.get('/todos', function (req, res, next) {
var todos= getTodos();
res.json(todos);
});
Getting started with node JS
What is MongoDB?
MongoDB is an Open-Source document-
oriented NoSQL database. It stores data in
JSON-like format.
Why use MongoDB ?
- SQL databases was invented to store data.
- MongoDB stores documents (or) Objects.
- now-a-days, everyone works with Objects
(Python/Ruby/Java/etc).
- And we need Databases to persist our objects.
- why not store objects directly?
- Embedded documents and arrays reduce need for Join.
MongoDB Tools
● Mongo : MongoDB client as a Javascript
shell.
● MongoImport : import CSV, JSON and TSV
data.
● MongoExport : export data as JSON and
CSV.
NoSQL DataBase Terms:
DataBase : Like other Relational DataBases.
Collection : Like Table in RDBMS(Has no
common Schema).
Document : Like a Record in RDBMS or Row.
Field : Like a RDBMS column {key : value }.
Mongoose CRUD(Create, Read, Update,
Delete)
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/MyApp');
var SpeakerSchema = new mongoose.Schema({
name : String ,
img : String,
detail : String
});
var Speaker = mongoose.model('Speaker',SpeakerSchema);
Create
app.post('/NewSpeaker',function(req,res){
var sp = new Speaker({
name : req.body.nameS,
img : req.body.imgS,
detail: req.body.det
});
sp.save(function(err){
if(err)
console.log(err);
else
res.json(sp);
});
});
Read
app.get('/getSpeakers/:Name',function(req,res){
Speaker.findOne({ name: req.params.Name }, function (err, doc){
if (err){
res.send(err);
}
res.json(doc);
});
});
app.get('/getSpeakers',function(req,res){
Speaker.find(function(err, todos) {
if (err){
res.send(err);
}
res.json(todos); // return all todos in JSON format
});
});
Update
● Model.update(conditions, update, [options],
[callback])
Delete
● Model.remove(conditions, [callback])
app.delete('/DeleteSpeaker/:id',function(req,res){
Speaker.remove({ _id : req.params.id}, function (err) {
if (err) return res.send(err);
res.json({"result":"success"});
});
});
<Coding Time !>
<Thank You!>
Hmidihamdi7@gmail.com
/+ HamdiHmidiigcien
/hamdi.igc

More Related Content

Getting started with node JS

  • 1. API Driven Applications AngularJS, NodeJS and MongoDB @HmidiHamdi Software Engeneering | ISSATSo Member & Founder | ISSATSo Google Club CXO | OMNIA
  • 2. Agenda - API Driven Application - Basics Of Node JS - Discovering MongoDB
  • 4. REST API REST = Representation State Transfer. - Client Sends HTTP verbs(GET, POST, DELETE, PUT) along with a URL and variable parameters that are unlencoded. - The URL tells us what object to act on. - Server Replies with a result code and valid JSON.
  • 5. HTTP Verbs - CRUD - GET : when a client want to read an object. - POST : when a client want to ceate an object. - PUT : when a client want to update an object. - DELETE : when a client want to delete an object.
  • 6. Why REST API ? a simple way to create a data service enabling you to easy create all your other applications: - HTML5 / JAVASCRIPT. - ANDROID. - IOS.
  • 7. MEAN STACK M : MongoDB (Most Popular NoSql DataBase). E : ExpressJS (Web Application Framework). A : AngularJS (A Robust Framework for creating HTML5 and Javascript rich web Applications). N : NodeJS (Server-side Javascript interpreter).
  • 9. What is NodeJS ? ● Open Source, Cross-platform runtime environment for server-side and networking applications. ● NodeJS provides an Event-Driven architecture and non-blocking I/O API. ● Run your Javascript on Server. ● Uses V8 Engine.
  • 10. Getting Started with Nodejs - Install / Build Nodejs - Type your Script - open Terminal and run script using : “node filename.js”
  • 11. What can you do with NodeJS - You can create an HTTP server and print ‘hello world’ on the browser in just 4 lines of JavaScript. - You can create a DNS server. - You can create a Static File Server. - You can create a Web Chat Application like GTalk in the browser. - Node.js can also be used for creating online games, collaboration tools or anything which sends updates to the user in real-time.
  • 12. Hello Node JS //On inclus la librairie http qui permet la création d'un serveur http basique var http = require('http'); http.createServer( function (request, response) { //Le type de contenu sera HTML, le code de réponse 200 (page correctement chargée) response.writeHead(200, {'Content-Type': 'text/html'}); response.end('Hello <a href="http://www.google.com.fr”>World</a> :pn'); } ).listen(666); //On affiche un petit message dans la console histoire d'être sûr que le serveur est lancé console.log('Visit http://localhost:666 ');
  • 15. Uses app.use(express.static(__dirname + '/Public')); // set the static files location /public/img will be /img for users app.use(morgan('dev')); // log every request to the console app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded app.use(bodyParser.json()); // parse application/json app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json app.use(methodOverride());
  • 16. Routes app.get('/todos/:id', function (req, res, next) { var todo=getTodo(id); res.json(todo); }); ------------- app.get('/todos', function (req, res, next) { var todos= getTodos(); res.json(todos); });
  • 18. What is MongoDB? MongoDB is an Open-Source document- oriented NoSQL database. It stores data in JSON-like format.
  • 19. Why use MongoDB ? - SQL databases was invented to store data. - MongoDB stores documents (or) Objects. - now-a-days, everyone works with Objects (Python/Ruby/Java/etc). - And we need Databases to persist our objects. - why not store objects directly? - Embedded documents and arrays reduce need for Join.
  • 20. MongoDB Tools ● Mongo : MongoDB client as a Javascript shell. ● MongoImport : import CSV, JSON and TSV data. ● MongoExport : export data as JSON and CSV.
  • 21. NoSQL DataBase Terms: DataBase : Like other Relational DataBases. Collection : Like Table in RDBMS(Has no common Schema). Document : Like a Record in RDBMS or Row. Field : Like a RDBMS column {key : value }.
  • 22. Mongoose CRUD(Create, Read, Update, Delete) var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/MyApp'); var SpeakerSchema = new mongoose.Schema({ name : String , img : String, detail : String }); var Speaker = mongoose.model('Speaker',SpeakerSchema);
  • 23. Create app.post('/NewSpeaker',function(req,res){ var sp = new Speaker({ name : req.body.nameS, img : req.body.imgS, detail: req.body.det }); sp.save(function(err){ if(err) console.log(err); else res.json(sp); }); });
  • 24. Read app.get('/getSpeakers/:Name',function(req,res){ Speaker.findOne({ name: req.params.Name }, function (err, doc){ if (err){ res.send(err); } res.json(doc); }); }); app.get('/getSpeakers',function(req,res){ Speaker.find(function(err, todos) { if (err){ res.send(err); } res.json(todos); // return all todos in JSON format }); });
  • 26. Delete ● Model.remove(conditions, [callback]) app.delete('/DeleteSpeaker/:id',function(req,res){ Speaker.remove({ _id : req.params.id}, function (err) { if (err) return res.send(err); res.json({"result":"success"}); }); });