This project is outdated. We've built it with Hapi.js v3.0 and Node v0.10.26.
There is a another similar example working with newer versions here: https://github.com/rcorral/hapi-restful-api-example
A sample structure for building a REST API on top of hapi.js framework that consumes a MySQL database.
Start by cloning this repo and going inside the project's folder:
$ git clone [email protected]:agendor/sample-hapi-rest-api.git
$ cd sample-hapi-rest-apiTo begin with, we have to define some environment variables (with an Ubuntu 12.04 you can edit the ~/.bashrc file):
export NODE_ENV=development
export NODE_HOST=localhost
export NODE_PORT=8000
export DB_DEV_USER=root
export DB_DEV_PASS=rootpassPS: Don't forget to alter DB_DEV_USER AND DB_DEV_PASS values properly to access your MySQL Database.
Then you have to give permission to the script database.sh:
$ chmod +x database.shAfter giving permission, you can run it. It's going to drop and then re-create a MySQL Database called hapi-todo with user and task tables and insert a default user, which credentials are:
- Email: [email protected]
- Pass: 123
So run it:
$ bash database.shDatabase setup. So it's time to install project dependencies, run the following command:
$ npm installNow you can check the recently created database in you MySQL. Start the server by running grunt in the terminal:
$ gruntAll the unit tests will run and everything should be green.
That's it! You should be able to play around your API GETting, POSTing, PUTing AND DELETEing Tasks. The end-points are as described in the src/routes/task.js file:
- GET http://localhost:8000/tasks/{task_id}
- GET http://localhost:8000/tasks
- POST http://localhost:8000/tasks
- PUT http://localhost:8000/tasks/{task_id}
- DELETE http://localhost:8000/tasks/{task_id}
If you want a deeper analysis of the project, checkout this Gist.