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
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
Swift Updates - Learn Languages 2025
koher
2
530
為你自己學 Python - 冷知識篇
eddie
1
360
Improving my own Ruby thereafter
sisshiki1969
1
170
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
660
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
560
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
120
ぬるぬる動かせ! Riveでアニメーション実装🐾
kno3a87
1
240
概念モデル→論理モデルで気をつけていること
sunnyone
3
310
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
11
4.5k
API Platform 4.2: Redefining API Development
soyuka
0
440
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
530
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
300
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
The Power of CSS Pseudo Elements
geoffreycrofte
77
6k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Large-scale JavaScript Application Architecture
addyosmani
513
110k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
31
2.2k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Optimizing for Happiness
mojombo
379
70k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
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/