|
2 | 2 |
|
3 | 3 | A [Yeoman](http://yeoman.io) generator for building a REST API in minutes using Express + MongoDB.
|
4 | 4 |
|
| 5 | +The main generator will scaffold a project that is identical to the express command line generator as of express 3.4.2. |
5 | 6 |
|
| 7 | +The express-rest generator adds the following: |
| 8 | +1 A resource generator for generating the routes and queries required to offer basic CRUD operations on the resource. |
| 9 | +2 Scaffolding for a connection to a MongoDB instance using the Mongoose driver. |
| 10 | +3 A basic logger using winston |
6 | 11 |
|
| 12 | +## Resource Generator |
| 13 | +The resource generator will generate the routes and queries required to offer basic CRUD operations on the resource. |
7 | 14 |
|
| 15 | +All CRUD APIs follow the convention: |
| 16 | +<table> |
| 17 | + <tr><th>Action</th><th>HTTP Verb</th><th>path</th></tr> |
| 18 | + <tr><td>Create</td><td>POST</td><td>/resource</td></tr> |
| 19 | + <tr><td>Read</td><td>GET</td><td>/resource/:id</td></tr> |
| 20 | + <tr><td>Update</td><td>PUT</td><td>/resource/:id</td></tr> |
| 21 | + <tr><td>Delete</td><td>DELETE</td><td>/resource/:id</td></tr> |
| 22 | + <tr><td>Get the full list</td><td>GET</td><td>/resource</td></tr> |
| 23 | +</table> |
| 24 | + |
| 25 | +If you wish to customize the code that has been generated, here's where to look: |
| 26 | +* The routes are defined in app.js |
| 27 | +* The Schema is defined in lib/db.js |
| 28 | +* The quries are defined in resource/<resource>.js |
| 29 | + |
| 30 | +## MongoDB Connection |
| 31 | +Mongoose is currently being used as the MongoDB driver. The file _lib/db.js_ contains the connection and schema definition. |
| 32 | + |
| 33 | +## Logging |
| 34 | +A basic logger is created, the details are in the file _lib/log.js_. By default it creates a log file named logs/development.log. If the environment variable NODE_ENV is set to 'production' then the file will be named logs/production.log. |
| 35 | + |
| 36 | +## Dependencies |
| 37 | +1 Make sure you have [yo](https://github.com/yeoman/yo) installed on the latest version. |
| 38 | + ```bash |
| 39 | + $ npm install -g yo |
| 40 | + ``` |
| 41 | +1 Ensure that [MongoDB](http://www.mongodb.org/) is installed and running. |
| 42 | + |
| 43 | +## Getting started |
| 44 | +1 Install the generator: `npm install -g generator-express-rest` |
| 45 | + ```bash |
| 46 | + $ npm install -g generator-express-rest |
| 47 | + ``` |
| 48 | +1 Create a project directory and run the generator |
| 49 | + ```bash |
| 50 | + $ mkdir myApp |
| 51 | + $ yo express-rest |
| 52 | + ``` |
| 53 | +1 Reply to the prompts asking you for your MongoDB connection details |
| 54 | + ```bash |
| 55 | + $ [?] IP address where MongoDB is running? (127.0.0.1) |
| 56 | + $ [?] Database name? (myApp) |
| 57 | + ``` |
| 58 | +1 Generate your first resource API |
| 59 | + ```bash |
| 60 | + $ yo express-rest:resource user |
| 61 | + ``` |
| 62 | +1 Start your node server |
| 63 | + ```bash |
| 64 | + $ node app.js |
| 65 | + ``` |
| 66 | +1 Create a few resources using curl |
| 67 | + ```bash |
| 68 | + $ curl -d "firstname=Brian" http://localhost:3000/users |
| 69 | + $ curl -d "firstname=Jerry" http://localhost:3000/users |
| 70 | + ``` |
| 71 | +1 Retreive the list resources using curl |
| 72 | + ```bash |
| 73 | + $ curl http://localhost:3000/users |
| 74 | + ``` |
| 75 | + |
| 76 | +**Happy Coding!** |
| 77 | + |
| 78 | +## License |
| 79 | +[MIT License](http://en.wikipedia.org/wiki/MIT_License) |
0 commit comments