Create a node.js application that uses MongoDB to store model data. The app has one REST endpoint - tips. This endpoint should handle:
- GET /tips/
- GET /tips/
- POST /tips/
- PUT /tips/
- DELETE /tips/
A ‘tip’ shall consist of:
- id (unique)
- Timestamp when tip was created
- Timestamp of last tip update
- Message
- Original message (first message submitted for a tip)
- Username of submitter
GET methods should implement a reasonable HTTP caching scheme.
This assumes that you have NodeJS and NPM installed via the command line. At time of writing, I am using the following versions:
- node v0.12.2
- npm v2.9.0
npm install
node server.js
I used Postman to test the API. Specifically the Chrome packaged app. With this, use the following keys:
- message
- username
- Chose not to use Mongo's _id, instead went for tip_id
- "use strict", should consider taking out for deployment
- The foreach in POST for /tips is naive, and more consideration should probably be had for partial updates? or batch-saving only if error-free run through
- I am particularly happy with the "createOrFetch" static method on the Tip model. It uses Node's paradigm of callbacks and error handling in a very fun way.