This is the 4th project for my 52 weeks, 52 projects challenge. It is a sample API developed with a Node.js framework called Sails. EDIT: For the 5th week of the challenge, I updated this repository with the authentication/authorization mechanism. I am still using Sails for building the API.
In order to use this application, you'll need Node.js and npm (the Node Package Manager) installed and also a local MongoDB instance. Make sure to import the exported MongoDB data:
$ mongoimport --db ships --collection ship --file ships.jsonEDIT: The above command won't work because the ships.json file is moved into another directory. Instead of that, you can seed the database with data for the ships and the users by executing the shells/v2/mongoseed.sh shell script. The data for the ships is not changed from the 4th week, but the data for the users is added on the 5th week. Every user has its username, password (bcrypt-ed), role (admin|editor for the moment), and an API key unique for each user. The API key is generated by md5-ing a concatenated string of the username and password (not the best idea for a production system, but simplicity FTW).
To install the dependencies and run the application, run:
$ npm install -g sails # install the Sails CLI
$ npm install # install the dependencies
$ sails lift # start the app serverFinally, browse to http://localhost:1337/api/v1/ship and see the application in action.
There are a couple of shell scripts included in this repository. I used them for testing the POST, PUT and DELETE functionality.
- The
shells/seeder.shfile seeds the database by actually sending aPOSTrequest, posting the JSON files in theshells/seed/folder. - The
shells/put.shfile sends aPUTrequest to update a given object. You should pass a JSON file as command-line argument (containing the updated data) and also the ID of the object. - The
shells/delete.shsends aDELETErequest to remove a given object from the DB. You should pass the ID of the object as command-line argument.
EDIT: The above scripts (with some changes) are placed in the shells/v1 directory. The main difference is that every shell script requires the API key passed as command-line argument. The seeder.sh is now post.sh (since it posts) and now requires a file to post into the server (choose from shells/v1/seed). In the 5th week I didn't use the now post.sh to seed the database. I just used mongoimport to import the previously-exported data in the JSON files in shells/v2/. I also added a shell script in the same directory to import the data.