For the past couple months I’ve been developing with Backbone.js and mocking data for an application. I’ve worked in the ecommerce industry for a few years and thought it would be a good idea to create a serious of posts on the topic of developing with Backbone using an example with some complexity, perhaps more than a ’todos’ or ’blog’ application, so the example will utilize a familiar Web application, an online store. To program a data-driven asynchronous application using a language I already know, JavaScript, the best way to learn is to write some code. So, I researched a few example applications using Node.js with a MongoDB database. This article is intended to be the first in a series on the topic building an online store using REST and Backbone.js to structure the code. This tutorial is not intended for production code, but rather an exploration of developing interactions with a RESTful API. This first post lays down a foundation for developing with a local API, then I can get into using the application with Backbone; but let’s get into the server-side for a bit first.
API Design for Mock Ecommerce Application
Goals for the Web service:
- Simple API design and pragmatic REST Web service, with only 2 base URLs per resource
- Keep verbs out of your base URLs
- Our HTTP verbs are POST, GET, PUT, and DELETE (Create, Read, Update, Delete)
- Concrete names are better than abstract
Example : two (2) resources (/products and /products/XX) and the four (4) HTTP verbs
| Resource | POST (create) |
GET (read) |
PUT (update) |
DELETE (delete) |
|---|---|---|---|---|
| /products | create a new product | list products | bulk update products | delete all products |
| /products/1234 | error | show 1234 | if exists update 1234, else error | delete 1234 |