-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (104 loc) · 3.02 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var cors = require("cors");
var redisClient = require("./utils/redis_wrapper");
var apicache = require("apicache");
var cache = apicache.options({
redisClient,
respectCacheControl: false,
}).middleware;
// Define routes
var indexRouter = require("./routes/index");
var catesRouter = require("./routes/cates");
var xyzPapersRouter = require("./routes/papers_gceguide_xyz");
var comPapersRouter = require("./routes/papers_gceguide_com");
var ppcoPapersRouter = require("./routes/pastpapers_co");
var yearsRouter = require("./routes/years");
var app = express();
// Setup views
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
// Setup middleware
const allowedOrigins = [
"https://www.snapaper.com",
"https://snapaper.com",
"http://localhost:3000",
];
app.use(
cors({
origin: function (origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg = "Access denied by CORS policies.";
return callback(new Error(msg), false);
}
return callback(null, true);
},
})
);
app.use(logger("dev"));
app.use(express.json());
app.use(
express.urlencoded({
extended: false,
})
);
app.use(cookieParser());
app.use(cache("1 month"));
// Define routes
app.use("/api", indexRouter);
app.use("/api/cates", catesRouter);
app.use("/api/papers/xyz", xyzPapersRouter);
app.use("/api/papers/com", comPapersRouter);
app.use("/api/papers/ppco", ppcoPapersRouter);
app.use("/api/years", yearsRouter);
app.get("/api/cache/clear", (_req, res) => {
res.json(apicache.clear());
});
// Catch 404 and forward to the error handler
app.use(function (_req, _res, next) {
next(createError(404));
});
// Error handler
app.use(function (err, req, res, _next) {
// Set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// Render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;
// var proxy = require("express-http-proxy");
// Delegate requests for directory listing to an external server
// app.use(
// "/case/cases",
// proxy("http://121.4.202.14", {
// proxyReqPathResolver: function (req) {
// return `/case/cases?cate=${req.query["cate"]}&sub=${req.query["sub"]}`;
// },
// })
// );
// Delegate requests for static files to an external server
// app.use(
// "/case/:cate/:sub/:paper",
// proxy("http://121.4.202.14", {
// proxyReqPathResolver: function (req) {
// return `/case/${req.params.cate}/${req.params.sub}/${encodeURI(
// req.params.paper
// )}`;
// },
// })
// );
// Delegate requests for remote file download to an external server
// app.use(
// "/download",
// proxy("http://121.4.202.14", {
// proxyReqPathResolver: function (req) {
// return `/download?filename=${encodeURI(req.query["filename"])}`;
// },
// })
// );