Attaching a file into a resource

Before we start receiving files in the Express server, we need to set up a strategy for that. We still want to use the RESTful services, so changing the format of the transmission data is not an option.

Respecting the RESTful standard (for more on the REST design for file uploads, see http://bit.ly/1GXqPNY), we can attach a subresource endpoint under the target resource to handle the uploads, so that it will not disturb the original resource. However, this approach has a limitation: the resource should exist first, which means that you cannot create a contact and its avatar photo at the same time.

Following this approach, the endpoint for the avatar file uploading can be located at:

http://example.com/api/contacts/10/avatar ...

Get Mastering Backbone.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.