Uploading the avatar photo to contacts
Let's start by creating the endpoint to upload avatar photos:
// routes.js
var controller = require('./controller');
//...
server.post('/api/contacts/:contactId/avatar',
controller.uploadAvatar);
Express itself does not process files automatically; it needs a plug-in that transforms the raw request into a more user-friendly API. This plug-in is named multer
; it processes multipart/form-data
, saving the file into a temporary path or making a buffer object, and then provides a JSON object with metadata information:
// Avatar endpoints var upload = multer(); server.post('/api/contacts/:contactId/avatar', upload.single('avatar'), controller.uploadAvatar ); server.use('/avatar', express.static(__dirname + '/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.