Created
January 24, 2018 13:23
-
-
Save geisonsn/2a7989d8a1b0542db0f5524e4d7baf46 to your computer and use it in GitHub Desktop.
Hapi Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Hapi = require('hapi'); | |
const Sequelize = require('sequelize'); | |
const sequelize = new Sequelize('alltism', 'root', 'root', { | |
host: 'localhost', | |
dialect: 'mysql', | |
pool: { | |
max: 5, | |
min: 0, | |
acquire: 30000, | |
idle: 1000 | |
}, | |
define : { | |
timestamps: false | |
} | |
}); | |
const User = sequelize.define('user', { | |
id: {type: Sequelize.INTEGER, primaryKey: true}, | |
name: Sequelize.STRING, | |
email: Sequelize.STRING, | |
username: Sequelize.STRING, | |
birthdate: Sequelize.DATE | |
}, { | |
freezeTableName: true | |
}); | |
const server = new Hapi.Server({port: 3000, host: 'localhost'}); | |
server.route({ | |
method: 'GET', | |
path: '/user', | |
handler: async function(request, h) { | |
const users = await User.findAll(); | |
if (users) return users; | |
return h.response().code(404); | |
} | |
}); | |
server.route({ | |
method: 'POST', | |
path: '/user', | |
handler: async function(request, h) { | |
await User.create(request.payload); | |
return h.response().code(201); | |
} | |
}); | |
server.route({ | |
method: 'PUT', | |
path: '/user/{id}', | |
handler: async function(request, h) { | |
await User.update(request.payload, { | |
where: { | |
id: request.params.id | |
} | |
}); | |
return h.response().code(204); | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/user/{id}', | |
handler: async function(request, h) { | |
const user = await User.findById(request.params.id); | |
if (user) return user; | |
return h.response().code(404); | |
} | |
}); | |
server.route({ | |
method: 'DELETE', | |
path: '/user/{id}', | |
handler: async function(request, h) { | |
await User.destroy({ | |
where: { | |
id: request.params.id | |
} | |
}); | |
return h.response().code(204); | |
} | |
}); | |
server.start(); | |
server.events.on('start', function() { | |
console.log(`Server started on port ${server.info.port}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "hapi-rest-exaple", | |
"version": "1.0.0", | |
"description": "hapi rest example", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"hapi": "^17.2.0", | |
"mysql2": "^1.5.1", | |
"sequelize": "^4.31.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment