-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
76 lines (59 loc) · 2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
var express = require("express"),
config = require("config"),
helpers = require("./src/helpers");
module.exports = function () {
var app = express();
app.use( express.compress() );
// configure logging
if ("development" == app.get("env")) {
app.use(express.logger("dev"));
} else if ("production" == app.get("env")) {
app.use(express.logger("default"));
}
// Configure some settings
app.set("json spaces", 2); // in production as well as in dev
// Intercept the static content
app.use("/", express.static(__dirname + "/static", { maxAge: 5 * 60 * 60 * 1000 }));
// Set up the default response
app.use(function (req, res, next) {
// By default don't cache anything (much easier when working with CloudFront)
res.header( "Cache-Control", helpers.cacheControl(0) );
// Allow all domains to request data (see CORS for more details)
res.header("Access-Control-Allow-Origin", "*");
next();
});
// Load the sub-apps
app.use("/v1/country", require("./src/country"));
app.use("/v1/books", require("./src/books"));
app.use("/v1/ping", require("./src/ping"));
app.get("/", function (req, res) {
res.header( "Cache-Control", helpers.cacheControl(3600) );
res.redirect("/v1");
});
app.get("/v1", function (req, res) {
var urlBase = config.api.protocol + "://" + config.api.hostport + "/v1/";
res.header( "Cache-Control", helpers.cacheControl(3600) );
res.jsonp({
books: urlBase + "books",
ping: urlBase + "ping",
});
});
// 404 everything that was not caught above
app.all("*", function (req, res) {
res.status(404);
res.jsonp({ error: "404 - page not found" });
});
// Default error handling - TODO change to JSON
app.use(function (error, req, res, next) {
if (error.status != 403) {
return next();
}
res.status(403);
res.jsonp({ error: "403 - forbidden"});
});
if ("development" == app.get("env")) {
app.use(express.errorHandler());
}
return app;
};