Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Restful on web service
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
SparrowJang
March 20, 2013
Programming
1
1k
Restful on web service
This example uses backbone.js and express.js.
SparrowJang
March 20, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.8k
MUSUBIXとは
nahisaho
0
130
Oxlint JS plugins
kazupon
1
530
AI 駆動開発ライフサイクル(AI-DLC):ソフトウェアエンジニアリングの再構築 / AI-DLC Introduction
kanamasa
12
6.5k
CSC307 Lecture 01
javiergs
PRO
0
680
Patterns of Patterns
denyspoltorak
0
1.4k
Package Management Learnings from Homebrew
mikemcquaid
0
200
CSC307 Lecture 05
javiergs
PRO
0
490
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
120
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
170
CSC307 Lecture 02
javiergs
PRO
1
770
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
2
2.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.8k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
GraphQLの誤解/rethinking-graphql
sonatard
74
11k
How GitHub (no longer) Works
holman
316
140k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
From π to Pie charts
rasagy
0
120
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
280
Unsuck your backbone
ammeep
671
58k
30 Presentation Tips
portentint
PRO
1
210
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
3.9k
The SEO Collaboration Effect
kristinabergwall1
0
350
Transcript
Restful on web service Sparrow Jang
None
HTTP Method method Data operate description GET read Get a
resource. POST create Create a resource without id. PUT update Update a resource or create a resource with id if not existed. DELETE delete Delete a resource
Restful URI action method uri show GET http://example/users/{id} list GET
http://example/users create POST http://example/users update PUT http://example/users/{id} delete DELETE http://example/users/{id}
Implement example • The backend uses node.js with express.js. •
backbone.js.
Express implements restful action code list app.get('/users', function( req, res
){ doSomething(); }); show app.get('/users/:id', function( req, res ){ doSomething(); }); create app.post('/users/:id', function( req, res ){ doSomething(); }); update app.put('/users/:id', function( req, res ){ doSomething(); }); delete app.delete('/users/:id', function( req, res ){ doSomething(); });
Show example app.get('/users/:id', function( req, res ){ var id =
req.params.id, result; if( id ) result = api.fetch( id ); res.send( result ); });
List example app.get('/users',function( req, res ){ var result = api.fetch();
res.send( result ); });
Backbone.js implements restful action code list collection.fetch(); show model.fetch(); create
collection.create({name:"Peter"},{wait: true}); model.save({name:"Peter"},{wait: true}); update model.save({name:"Peter"}); delete model.destroy();
Model example var User = Backbone.Model.extend({ //set path urlRoot :
'/users', //validate attrs by set or save validate:function( attrs, options ){ if( !attrs.name ) return "name is null."; }, ...
Response data from server Get /users/1 { "result": { "success":
true }, "user": { "id": 1, "name": "Tom" } }
... parse:function( response, options ){ //from server if( response.result &&
response.result.success ) return response.user; //from collection else if( !response.result ) return response } });
Response data from server Get /users { "result": { "success":
true }, "users": [ {"id": 1,"name": "Tom"}, {"id": 2,"name": "John"}, ...] }
Collection example var Users = Backbone.Collection.extend({ url: '/users', model:User, parse:function(
response, options ){ if( response.result.success ) return response.users; } });
The End 1. github 2. http://sparrowhome.twbbs.org/wordpress/