-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
56 lines (45 loc) · 1.15 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
48
49
50
51
52
53
54
55
56
'use strict';
var _debug = require('debug');
var Hapi = require('hapi');
var util = require('util');
var xml2json = require('xml2json');
var meta = require('./package.json');
var debug = _debug(util.format('%s:http', meta.name));
var server = new Hapi.Server({
connections: {
routes: {
cors: true
}
},
debug: process.env.MACROCOSM_DEBUG ? {
log: [ 'error' ],
request: [ 'error', 'received', 'response' ]
} : false
});
server.connection({ port: process.env.PORT || 4000 });
server.ext('onRequest', function(req, res) {
debug('%s %s', req.method.toUpperCase(), req.url.href);
return res.continue();
});
// Register routes
server.register({
register: require('hapi-router'),
options: {
routes: 'routes/*.js'
}
}, function (err) {
if (err) throw err;
});
server.ext('onPostAuth', function(req, res) {
if (req.mime === 'text/xml' && Object.keys(req.payload || {}).length > 0) {
debug(req.payload);
req.payload = xml2json.toJson(req.payload, {
object: true
});
}
return res.continue();
});
server.start(function () {
console.log('Server running at:', server.info.uri);
});
module.exports = server;