-
Notifications
You must be signed in to change notification settings - Fork 1
/
directory-and-auth.js
40 lines (34 loc) · 1.26 KB
/
directory-and-auth.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
40
var Hapi = require('hapi');
var server = new Hapi.Server(8000, { files: { relativeTo: __dirname } });
function validate (username, password, callback) {
if (username == 'walmartlabs' && password == 'walmartlabs') {
return callback(null, true, {username: username});
} else {
return callback("invalid credentials", false);
}
}
// hapi-auth-basic is required, enables hapi to use basic http authentication
server.pack.register([require('hapi-auth-basic')], function (err){
server.auth.strategy('basic', 'basic', { validateFunc: validate });
/* go to:
http://localhost:8000/public/img/hapi.png
http://localhost:8000/private/txt/passwd.txt
*/
server.route([
{ method: 'GET', path: '/public/{file*}', handler: { directory: { path: './directory/public/', listing: true } } },
{ method: 'GET', path: '/private/{file*}', config: {
auth: {
strategies: ['basic']
},
handler: {
directory: {
path: './directory/private/',
listing: true
}
}
}}
]);
server.start(function(){
console.log('server started on port', server.info.port);
});
});