Last active
August 29, 2015 14:00
-
-
Save mikl/11398231 to your computer and use it in GitHub Desktop.
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
/* | |
Copyright (c) 2014, Mikkel Hoegh <[email protected]> | |
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
var Hapi = require('hapi'); | |
var Joi = require('joi'); | |
var server = new Hapi.Server(6300); | |
var helloHandler = function (request, reply) { | |
reply({ hello: 'Simple, eh?' }); | |
}; | |
server.route({ | |
method: 'GET', | |
path: '/hello', | |
handler: helloHandler | |
}); | |
var ponyHandler = function (request, reply) { | |
reply({ 'Selected pony': request.query.name}); | |
}; | |
server.route({ | |
method: 'GET', | |
path: '/pony', | |
config: { | |
handler: ponyHandler, | |
validate: { | |
query: { | |
name: Joi.string().min(3).required() | |
} | |
} | |
}, | |
}); | |
var ponyFoodHandler = function (request, reply) { | |
reply({ | |
ponyId: request.params.id, | |
food: request.params.foodName, | |
}); | |
}; | |
server.route({ | |
method: 'GET', | |
path: '/pony/{id}/food/{foodName}', | |
config: { | |
handler: ponyFoodHandler, | |
validate: { | |
path: { | |
id: Joi.number().integer().min(10).required(), | |
foodName: Joi.string().required() | |
} | |
} | |
}, | |
}); | |
server.start(); | |
console.log('I am listening on http://127.0.0.1:6300'); |
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.js Demo", | |
"author": "Mikkel Hoegh <[email protected]> (http://mikkel.hoegh.org/)", | |
"private": true, | |
"version": "0.0.0", | |
"description": "Demo for my Hapi.js presentation", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"license": "ISC", | |
"dependencies": { | |
"hapi": "^4.0.3", | |
"joi": "^3.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment