-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
57 lines (45 loc) · 1.16 KB
/
server.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
57
const Hapi = require("hapi");
const HapiRouter = require("hapi-router");
const VERSION = require("./package.json").version;
const server = new Hapi.Server({
connections: {
routes: {
cors: true
}
}
});
server.connection({ port: process.env.API_PORT || process.env.PORT || 3000 });
// Register routes
server.register(
{
register: HapiRouter,
options: {
routes: "routes/*.js"
}
},
err => {
if (err) throw err;
}
);
server.ext("onRequest", (req, res) => {
console.log(
`${req.info.remoteAddress}: ${req.method.toUpperCase()} ${req.url.path}`
);
return res.continue();
});
server.ext("onPreResponse", (req, reply) => {
const { response } = req;
if (response.isBoom) {
response.output.headers["X-API-Version"] = VERSION;
} else {
response.header("X-API-Version", VERSION);
}
return reply.continue();
});
server.on("request-error", (req, err) => {
console.warn(`${req.method.toUpperCase()} ${req.url.path}`);
console.warn(err.stack);
});
server.start(() => console.log("Server running at:", server.info.uri));
process.on("unhandledRejection", err => console.warn(err.stack));
module.exports = server;