forked from agendor/sample-hapi-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (38 loc) · 1.03 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
40
41
42
43
44
45
46
47
"use strict";
var Hapi = require('hapi');
var constants = require('src/config/constants.js');
var basicAuth = require('src/middleware/basic-auth');
var routes = require('src/routes');
var _ = require('underscore');
var options = {
state : {
cookies : {
strictHeader : false
}
}
};
var host = constants.application['host'];
var port = constants.application['port'];
var server = Hapi.createServer(host, port, options);
server.pack.require('hapi-auth-basic', function (err) {
server.auth.strategy('simple', 'basic', true, {
validateFunc: basicAuth
});
});
server.ext('onRequest', function(request, next){
request.plugins.createControllerParams = function(requestParams){
var params = _.clone(requestParams);
params.userId = request.auth.credentials.userId;
return params;
};
next();
});
// Add all the routes within the routes folder
for (var route in routes) {
server.route(routes[route]);
}
module.exports = server;
if (process.env.NODE_ENV !== 'test') {
server.start();
console.log('Server running in port #'+port);
}