forked from dwyl/learn-hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (33 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Start this app from your command line with: node hellovalidate.js
// then visit: http://localhost:3000/YOURNAME
var Hapi = require('hapi');
var Joi = require('joi');
var Boom = require('boom');
var port = 3000; // process.env.PORT || 3000; // allow port to be set by environment
var server = new Hapi.Server();
server.connection({ port: port });
server.route({
method: ['GET', 'POST'],
path: '/{name*}',
config: { // validate will ensure YOURNAME is valid before replying to your request
validate: { params: { name: Joi.string().max(40).min(2).alphanum() } },
handler: function (request, reply) {
reply('Hai '+ request.params.name + '!');
}
}
});
server.route({
method: 'GET',
path: '/photo/{id*}',
config: { // validate will ensure YOURNAME is valid before replying to your request
validate: { params: { id: Joi.string().max(40).min(2).alphanum() } },
handler: function (request, reply) {
// until we implement authentication we are simply returning a 401:
reply(Boom.unauthorized('Please log-in to see that'));
}
}
});
server.start(function() {
console.log('Now Visit: http://localhost:' + port + '/YOURNAME');
});
module.exports = server;