├── .gitignore ├── README.md ├── index.js ├── package.json └── src ├── index.js ├── models └── pizza.js └── routes └── pizza.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | .tmp 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hapi REST API starter 2 | 3 | Check out the corresponding blog post ["How to create a REST API with Hapi"](http://blog.webkid.io/how-to-create-a-rest-api-with-hapi/) if you are interested in getting to know more about creating REST APIs with Hapi. 4 | 5 | ## Installation 6 | 7 | ``` 8 | $ npm install 9 | ``` 10 | 11 | ## Start Server 12 | 13 | ``` 14 | $ npm start 15 | ``` 16 | 17 | ## What do you get? 18 | 19 | A simple pizza REST API: 20 | 21 | ``` 22 | http://localhost:1337 23 | GET /pizza 24 | POST /pizza 25 | GET /pizza/{id} 26 | POST /pizza/{id} 27 | PATCH /pizza/{id} 28 | DELETE /pizza/{id} 29 | ``` 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('./src'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hapi-rest-starter", 3 | "version": "1.0.0", 4 | "description": "A simple Hapi project that offers you REST API based on Dogwater and Bedwetter.", 5 | "main": "index.js", 6 | "repository" : "https://github.com/wbkd/hapi-rest-starter-simple", 7 | "scripts": { 8 | "start" : "node index.js" 9 | }, 10 | "author": "http://webkid.io", 11 | "contributors": [ 12 | "http://github.com/moklick" 13 | ], 14 | "license": "MIT", 15 | "dependencies": { 16 | "bedwetter": "^1.8.2", 17 | "blipp": "^2.2.1", 18 | "dogwater": "^0.4.7", 19 | "hapi": "^8.8.0", 20 | "sails-disk": "^0.10.8", 21 | "shortid": "^2.2.2" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | var Hapi = require('hapi'); 2 | 3 | var server = new Hapi.Server(); 4 | server.connection({ port: 1337, host: 'localhost' }); 5 | 6 | var dogwaterOptions = { 7 | connections: { 8 | pizzaDB : { 9 | adapter: 'pizzaDisk' 10 | } 11 | }, 12 | adapters:{ 13 | pizzaDisk : 'sails-disk' 14 | }, 15 | models: [require('./models/pizza')] 16 | }; 17 | 18 | server.register([{ 19 | register : require('blipp') 20 | },{ 21 | register: require('dogwater'), 22 | options: dogwaterOptions 23 | },{ 24 | register: require('bedwetter'), 25 | options: {} 26 | } 27 | ], function (err) { 28 | if (err) { return console.log(err); } 29 | 30 | server.route(require('./routes/pizza')); 31 | 32 | server.start(function () { 33 | console.log('Pizza API up and running at:', server.info.uri); 34 | }); 35 | }); 36 | 37 | -------------------------------------------------------------------------------- /src/models/pizza.js: -------------------------------------------------------------------------------- 1 | var shortid = require('shortid'); 2 | 3 | module.exports = { 4 | identity: 'pizza', 5 | connection: 'pizzaDB', 6 | attributes: { 7 | id: { 8 | type: 'string', 9 | primaryKey: true, 10 | unique: true, 11 | defaultsTo: function() { 12 | return shortid.generate(); 13 | } 14 | }, 15 | name: { 16 | type : 'string', 17 | required : true 18 | }, 19 | ingredients: { 20 | type : 'array', 21 | required : false 22 | } 23 | } 24 | }; -------------------------------------------------------------------------------- /src/routes/pizza.js: -------------------------------------------------------------------------------- 1 | var bedwetterOptions = {} 2 | 3 | module.exports = [{ 4 | // return all pizza items 5 | path: '/pizza', 6 | method: 'GET', 7 | config: { 8 | handler: { 9 | bedwetter: bedwetterOptions 10 | } 11 | } 12 | }, { 13 | // return a specific pizza by id 14 | path: '/pizza/{id}', 15 | method: 'GET', 16 | config: { 17 | handler: { 18 | bedwetter: bedwetterOptions 19 | } 20 | } 21 | }, { 22 | // create a new pizza 23 | path: '/pizza', 24 | method: 'POST', 25 | config: { 26 | handler: { 27 | bedwetter: bedwetterOptions 28 | } 29 | } 30 | }, { 31 | // udpate an existing pizza by id 32 | path: '/pizza/{id}', 33 | method: ['PATCH', 'POST'], 34 | config: { 35 | handler: { 36 | bedwetter: bedwetterOptions 37 | } 38 | } 39 | }, { 40 | // remove a pizza by id 41 | path: '/pizza/{id}', 42 | method: 'DELETE', 43 | config: { 44 | handler: { 45 | bedwetter: bedwetterOptions 46 | } 47 | } 48 | }]; 49 | --------------------------------------------------------------------------------