This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (104 loc) · 2.98 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
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
119
120
121
122
123
124
var express = require("express");
var morgan = require("morgan");
var app = express();
var mongoose = require("mongoose");
var models = require("./server/models");
var paginate = require("./server/middlewares/custom-paginate");
var cors = require("cors");
var history = require("connect-history-api-fallback");
//var bodyParser = require("body-parser");
// My middlewares
var injectPaginate = require("./server/middlewares/inject-paginate");
var searchParams = require("./server/middlewares/search-params");
// Require configuration file defined in app/Config.js
var config = require("./server/config");
// Connect to database
mongoose.connect(
config.db.uri,
{
useNewUrlParser: true,
auth: { user: config.db.username, password: config.db.password }
}
);
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on("error", console.error.bind(console, "MongoDB connection error:"));
// Use morgan to log request in dev mode
//app.use(morgan("dev"));
app.use(cors());
app.use(paginate.middleware(10, 50));
// Extract search params from URL parameters in req.custom.searchParams
app.use(searchParams);
app.get("/api/decp/sources", async (req, res, next) => {
if (!req.custom) {
req.custom = {};
}
try {
const [results, totalCount] = await Promise.all([
models.Source.find(res.locals.searchParams)
.limit(req.query.per_page || req.query.limit)
.skip(req.skip)
.lean()
.exec(),
models.Source.countDocuments(res.locals.searchParams)
]);
res.locals.results = results;
res.locals.totalCount = totalCount;
next();
} catch (err) {
next(err);
}
});
app.get("/api/decp/marches", async (req, res, next) => {
if (!req.custom) {
req.custom = {};
}
try {
const [results, totalCount] = await Promise.all([
models.Marche.find(res.locals.searchParams)
.sort(req.query.sort)
.limit(req.query.per_page || req.query.limit)
.skip(req.skip)
.lean()
.exec(),
models.Marche.countDocuments(res.locals.searchParams)
]);
res.locals.results = results;
res.locals.totalCount = totalCount;
next();
} catch (err) {
next(err);
}
});
app.get("/api/decp/stats", async (req, res, next) => {
if (!req.custom) {
req.custom = {};
}
try {
const [results, totalCount] = await Promise.all([
models.Stat.find(res.locals.searchParams)
.sort(req.query.sort)
.limit(req.query.per_page || req.query.limit)
.skip(req.skip)
.lean()
.exec(),
models.Stat.countDocuments(res.locals.searchParams)
]);
res.locals.results = results;
res.locals.totalCount = totalCount;
next();
} catch (err) {
next(err);
}
});
app.use(injectPaginate);
app.use(
history({
verbose: false
})
);
// Sends static files from the public path directory
app.use(express.static("dist"));
module.exports = app;