Node.js: Building a RESTful API
Nikos Kampitakis
Stavros Bastakis
September 10, 2015
Heraklion
Who is who
Stavros Bastakis:
- Web developer @ AdScience
- Three months of professional Node.js experience
- PHP background
Nikos Kampitakis:
- Software developer @ imgZine
- Founder @ webtrails
- One year of professional Node.js experience
- PHP previous years
What is Node.js
Node.js is
- a runtime environment
- for developing server-side web applications
- using Javascript
- open-source & cross-platform
- uses an event-driven & non-blocking I/O model
How it works
Pitfalls
- CPU-intensive operations
- Exceptions bubbling up to the core event-loop
- Callback hell
Why Node.js (1/3)
- Asynchronous, event-driven
- Powered by V8. JavaScript not interpreted but compiled
- Performance in data-intensive operations
- Fact 1. PayPal reported: double the number of requests per-second and reduced response time by 35% or 200 milliseconds.
- Fact 2. On Black Friday the WalMart servers didn’t go over 1% CPU utilisation and the team did a deploy with 200,000,000 users online
Why Node.js (2/3)
- Unified API: Node.js combined with a browser, a document DB (mongodb) and JSON offers a unified JavaScript development stack.
- Even same libraries server and client side (see moment.js)
- Isomorphic (aka Universal) development
- One large pool of modules (npm) VS modules per platform
- Start with a small collection of modules and build up as you go VS large monolithic frameworks
Why Node.js (3/3)
- Built-in support for package management using the NPM tool that comes by default with every Node.js installation
- Large and fast-evolving community creates great modules/projects
- Shorter development cycles
- Developers love it!
The Callback Hell
- What is a callback function?
- Used for asynchronous calls
Example
Async lib to the rescue
async.series([ function(callback){ // do some stuff ... callback(null, 'one'); }, function(callback){ // do some more stuff ... callback(null, 'two'); } ], // optional callback function(err, results){ // results is now equal to ['one', 'two'] });
- Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript.
- Async provides around 20 functions that include the usual 'functional' suspects (map, reduce,filter, each…) as well as some common patterns for asynchronous control flow (parallel,series, waterfall…).
- Example:
Usefull node modules
- Express - web framework for Node.js
- Request - make HTTP calls easily from your Node.js app
- Lodash - toolkit of Javascript functions for manipulating objects and collections
-
Moment - Full featured library for manipulating dates
-
Passport - authentication middleware for Node.js
- Want more..?
http://www.npmjs.com
RESTful API
Project: Task list
- GET /tasks : list all the tasks
- POST /tasks : create new task
- GET /tasks/{task_id} : get a task
- PUT /tasks/{task_id} : edit a task
- DELETE /tasks/{task_id} : delete a task
Postman
Postman is a Chrome Addon that helps us build, test, and document our APIs faster. It provides en easy way to build and store requests and a nice interface to check the responses.
Node Inspector
Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools (formerly WebKit Web Inspector).
$ npm install -g node-inspector
$ nodemon --debug app.js
$ node-inspector app.js
Related topics
- Template engines: Handlebars - Jade - Swig
- MongoDB
- Promises
Thank you!
Nikos Kampitakis
@kabitakis
gr.linkedin.com/in/kabitakis
nikos@webtrails.gr
Stavros Mpastakis
@sstauross
linkedin.com: Stavros Bastakis
staurosmpa@gmail.com
Node.js: Building a RESTful API
By Nikos Kampitakis
Node.js: Building a RESTful API
- 1,509