Express server
To demonstrate how to send files to a server, in this chapter we will work with the latest version of Express (the latest version available at the time of writing is Express 4.x). The server will be responsible for storing the REST resources and handling file uploads. Please consult the GitHub repo for this book to get the implementation of the server for the previous chapters.
For now, the current server is able to create, get, update, and delete contact resources; we need to add a mechanism to upload an avatar image for a contact. For simplicity the application does not use a database to store its data, but instead uses a hash table to store all data in memory. For example, the next snippet demonstrates how to store a contact:
// Insert a new contact JSON into the contacts array createContact(req, res) { var contact = extractContactData(req); // Asssign a random id contact.id = makeId(); contacts.push(contact); res.status(201) .json(contact); }