Skip to content

Commit

Permalink
makemehapi validation using joi object. instructions are bad!
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 6, 2015
1 parent efde67f commit 49af24b
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 0 deletions.
15 changes: 15 additions & 0 deletions makemehapi/01-HELLO_HAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

function hellohandler(request, reply) {
reply("Hello Hapi");
}

server.route({path: '/', method:'GET', handler: hellohandler});

server.start();
15 changes: 15 additions & 0 deletions makemehapi/02-ROUTES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

function routehandler(request, reply) {
reply('Hello ' + encodeURIComponent(request.params.name));
}

server.route({path: '/{name*}', method:'GET', handler: routehandler});

server.start();
15 changes: 15 additions & 0 deletions makemehapi/03-HANDLING.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

server.route({path: '/{name*}', method:'GET',
handler: {
file: "index.html"
}
});

server.start();
19 changes: 19 additions & 0 deletions makemehapi/04-DIRECTORIES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

function hellohandler(request, reply) {
reply('Hello ' + encodeURIComponent(request.params.name));
}

server.route({path: '/{name*}', method:'GET',
handler: {
directory: { path: __dirname }
}
});

server.start();
23 changes: 23 additions & 0 deletions makemehapi/05-VIEWS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var Hapi = require('hapi');
var Path = require('path');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

server.route({path: '/{name*}', method:'GET',
handler: {
view: "index.html"
}
});

server.views({
engines: {
html: require('handlebars')
},
path: Path.join(__dirname, 'templates')
});

server.start();
18 changes: 18 additions & 0 deletions makemehapi/06-PROXIES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var Hapi = require('hapi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

server.route({path: '/proxy', method:'GET',
handler: {
proxy: {
host: '127.0.0.1',
port: 65535
}
}
});

server.start();
24 changes: 24 additions & 0 deletions makemehapi/07-HELPING.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Hapi = require('hapi');
var Path = require('path');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

server.route({path: '/', method:'GET',
handler: {
view: "helper-index.html"
}
});

server.views({
engines: {
html: require('handlebars')
},
path: Path.join(__dirname, 'templates'),
helpersPath: Path.join(__dirname, 'helpers')
});

server.start();
24 changes: 24 additions & 0 deletions makemehapi/08-STREAMS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Fs = require('fs');
var Hapi = require('hapi');
var Path = require('path');
var Rot13 = require('rot13-transform');

var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

server.route({
method: 'GET',
path: '/',
config: {
handler: function (request, reply) {
var thisfile = Fs.createReadStream(Path.join(__dirname, 'input.txt'));
reply(thisfile.pipe(Rot13()));
}
}
});

server.start();
30 changes: 30 additions & 0 deletions makemehapi/09-VALIDATION.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var Hapi = require('hapi');
var Joi = require('joi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

function myHandler(request, reply) {
reply('Hello Joi!');
}

var routeConfig = {
path: '/a/path/{with}/{parameters}',
method: 'GET',
handler: myHandler,
config: {
validate: {
params: {
with: Joi.string().required(),
parameters: Joi.string().required()
}
}
}
}

server.route(routeConfig);

server.start();
36 changes: 36 additions & 0 deletions makemehapi/10-VALIDATION-USING-JOI-OBJECT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var Hapi = require('hapi');
var Joi = require('joi');
var server = new Hapi.Server();

server.connection({
host: 'localhost',
port: Number(process.argv[2] || 8080)
});

function myHandler(request, reply) {
reply('login successful');
}

var routeConfig = {
path: '/login',
method: 'POST',
config: {
handler: myHandler,
validate: {
payload: Joi.object({
username: Joi.string(),
password: Joi.string().alphanum(),
accessToken: Joi.string().alphanum(),
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email()
})
.options({allowUnknown: true})
// .with('username', 'birthyear')
.without('password', 'accessToken')
}
}
}

server.route(routeConfig);

server.start();
6 changes: 6 additions & 0 deletions makemehapi/foo/bar/baz/file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head><title>Hello Directories</title></head>
<body>
Hello Directories
</body>
</html>
4 changes: 4 additions & 0 deletions makemehapi/helpers/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function(context) {
var query = context.data.root.query;
return query.name + query.suffix;
}
6 changes: 6 additions & 0 deletions makemehapi/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head><title>Hello Handling</title></head>
<body>
Hello Handling
</body>
</html>
1 change: 1 addition & 0 deletions makemehapi/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The Pursuit of Hapi-ness
6 changes: 6 additions & 0 deletions makemehapi/templates/helper-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head><title>Hello {{helper}}</title></head>
<body>
Hello {{helper}}
</body>
</html>
6 changes: 6 additions & 0 deletions makemehapi/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head><title>Hello {{query.name}}</title></head>
<body>
Hello {{query.name}}
</body>
</html>

0 comments on commit 49af24b

Please sign in to comment.