A
P
I
Patrick
Heneise
Apis,Egyptiangodofstrengthandfertility
http://icity-devp.icityproject.com
Let’stalkabout
yourAPI
WhatmakesagoodAPI?
★ Performance
★ Scalability
★ Reusability
★ Evolvability
★ Documentation
★ Testability
★ Value
★ Easyto...
‘OnceyouhaveabadAPI,youcannotchange
it.Forever.You’vegotonechancehowto
makeitright.’–JoshuaBloch,GoogleTechTalk,Jan2007
Whynode.jsisthe
righttool!
★ Double-clicktoedit.
It’snot.
Chooseyour
weaponscarefully!
Butnode.jshassome
nicefeatures.
Andit’scool.
Becauseit’sJavaScript.
AndJavaScriptiscool,
right?
Solet’screatean
APIinnode.js
TheDemoAPI
TheCatAPI!
Seriously?
So,introducing:
TheJunkAPI
https://github.com/
PatrickHeneise/TheJunkAPI
‘AnAPIshoulddoonething,andtoitwell.’
JoshuaBloch,GoogleTechTalk,Jan2007
Adding,listing,
showinganddeleting:
Junk.
app.get('/api/junks',junk.index);
app.get('/api/junk/:id',junk.show);
app.post('/api/junk',junk.create);
app.delete('/api/...
exports.show=function(req,res){
varid=req.params.id;
redis.hgetall('junk:'+id,function(error,junk){
if(junk===null){
res.s...
Input/Output
★ params:/api/junk/1
★ query:/api/junk?id=1
★ body:/api/junk-d{‘id’:1}
★ Chooseyourweaponcarefully!
Input
★ HTTPStatusCodes
★ ResponseBody
★ Usethestandards!
Output
★ 200-Success
★ 201-Created
★ 204-Nocontent
★ 401-Unauthorized
★ 403-Forbidden
★ 404-NotFound
★ 409-Conflict
★ 500-Internal...
res.send(200,{});
Authentication
passport.js
★ OAuth
★ OAuth2
★ apikey
★ HTTPBasic
★ HTTPDigest
★ ...
Again,chooseyour
weaponcarefully!
Testing
★ mocha
http://visionmedia.github.io/
mocha/
★ vows
http://vowsjs.org
★ specify
https://github.com/dscape/
specify
mocha
describe('GET /junk/:id', function () {
it('should return details of the junk', function (done) {
request(url)
.get('/junk...
Prepareyour
testing
environment!
‘Andcleanupafterwards!’-Mum
before(function(done){
! require('./before').before(function(){
! ! done();
! })
});
after(function(done){
! require('./af...
Junk
GET /junks
✓ should return a list of junk
GET /junk/:id
✓ should return details of the junk
✓ should fail with 404
PO...
Yay.✓
Documentation
★ http://jsdox.org
★ https://github.com/visionmedia/
dox-JavaScriptdocumentation
generator
★ https://github.com/mashery/
i...
Dox
/**
*Outputasinglepieceofjunk
specifiedby:id
*
*@param{Number}id
*@return{Object}Alljunkdetails
*@apipublic
*/
iodocs
Resources
(nomorecats)
★ HowToDesignAGoodAPIandWhy
itMattershttp://www.youtube.com/watch?v=aAb7hSCtvGw http://lcsd05.cs.tamu.edu/
slides/keynote....
Thankyou.
PatrickHeneise
@PatrickHeneise
http://patrickheneise.com
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Api.js
Upcoming SlideShare
Loading in …5
×

Api.js

500 views

Published on

In a world of mobile apps, single-page applications and flying web servers, JavaScript, Objective-C, Java and whats-so-not consumers, solid APIs are getting more and more important. This talk is about how to create, test and document an API in Node.js with Express, Mocha, Dox and iodocs.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
500
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Api.js

  1. 1. A P I Patrick Heneise
  2. 2. Apis,Egyptiangodofstrengthandfertility
  3. 3. http://icity-devp.icityproject.com
  4. 4. Let’stalkabout yourAPI
  5. 5. WhatmakesagoodAPI? ★ Performance ★ Scalability ★ Reusability ★ Evolvability ★ Documentation ★ Testability ★ Value ★ Easytolearn&use ★ Hardtomisuse
  6. 6. ‘OnceyouhaveabadAPI,youcannotchange it.Forever.You’vegotonechancehowto makeitright.’–JoshuaBloch,GoogleTechTalk,Jan2007
  7. 7. Whynode.jsisthe righttool! ★ Double-clicktoedit.
  8. 8. It’snot.
  9. 9. Chooseyour weaponscarefully!
  10. 10. Butnode.jshassome nicefeatures. Andit’scool. Becauseit’sJavaScript. AndJavaScriptiscool, right?
  11. 11. Solet’screatean APIinnode.js
  12. 12. TheDemoAPI
  13. 13. TheCatAPI!
  14. 14. Seriously?
  15. 15. So,introducing: TheJunkAPI https://github.com/ PatrickHeneise/TheJunkAPI
  16. 16. ‘AnAPIshoulddoonething,andtoitwell.’ JoshuaBloch,GoogleTechTalk,Jan2007
  17. 17. Adding,listing, showinganddeleting: Junk.
  18. 18. app.get('/api/junks',junk.index); app.get('/api/junk/:id',junk.show); app.post('/api/junk',junk.create); app.delete('/api/junk/:id',junk.remove);
  19. 19. exports.show=function(req,res){ varid=req.params.id; redis.hgetall('junk:'+id,function(error,junk){ if(junk===null){ res.send(404); }else{ res.send(200,junk); } }); };
  20. 20. Input/Output
  21. 21. ★ params:/api/junk/1 ★ query:/api/junk?id=1 ★ body:/api/junk-d{‘id’:1} ★ Chooseyourweaponcarefully! Input
  22. 22. ★ HTTPStatusCodes ★ ResponseBody ★ Usethestandards! Output
  23. 23. ★ 200-Success ★ 201-Created ★ 204-Nocontent ★ 401-Unauthorized ★ 403-Forbidden ★ 404-NotFound ★ 409-Conflict ★ 500-InternalServer Error HTTPStatusCodes
  24. 24. res.send(200,{});
  25. 25. Authentication
  26. 26. passport.js
  27. 27. ★ OAuth ★ OAuth2 ★ apikey ★ HTTPBasic ★ HTTPDigest ★ ...
  28. 28. Again,chooseyour weaponcarefully!
  29. 29. Testing
  30. 30. ★ mocha http://visionmedia.github.io/ mocha/ ★ vows http://vowsjs.org ★ specify https://github.com/dscape/ specify
  31. 31. mocha
  32. 32. describe('GET /junk/:id', function () { it('should return details of the junk', function (done) { request(url) .get('/junk/0') .set('apikey', 'asdasjsdgfjkjhg') .send() .end(function (error, response) { response.status.should.equal(200); response.body.text.should.equal('iz likin myself on lolcats.com'); should.exist(response.body.url); done(error); }); });
  33. 33. Prepareyour testing environment!
  34. 34. ‘Andcleanupafterwards!’-Mum
  35. 35. before(function(done){ ! require('./before').before(function(){ ! ! done(); ! }) }); after(function(done){ ! require('./after').after(ids,function(){ ! ! done(); ! }); });
  36. 36. Junk GET /junks ✓ should return a list of junk GET /junk/:id ✓ should return details of the junk ✓ should fail with 404 POST /junk ✓ should create new junk ✓ should fail to create new junk DELETE /junk/:id ✓ should delete junk, huzzah! ✓ should fail to delete junk 7 passing (1s)
  37. 37. Yay.✓
  38. 38. Documentation
  39. 39. ★ http://jsdox.org ★ https://github.com/visionmedia/ dox-JavaScriptdocumentation generator ★ https://github.com/mashery/ iodocs-I/ODocs-OpenSourcein Node.js
  40. 40. Dox
  41. 41. /** *Outputasinglepieceofjunk specifiedby:id * *@param{Number}id *@return{Object}Alljunkdetails *@apipublic */
  42. 42. iodocs
  43. 43. Resources
  44. 44. (nomorecats)
  45. 45. ★ HowToDesignAGoodAPIandWhy itMattershttp://www.youtube.com/watch?v=aAb7hSCtvGw http://lcsd05.cs.tamu.edu/ slides/keynote.pdf ★ http://stackoverflow.com/ questions/2619854/best-practices- and-guidelines-for-designing-an- api ★ https://dev.twitter.com//
  46. 46. Thankyou. PatrickHeneise @PatrickHeneise http://patrickheneise.com

×