-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (62 loc) · 1.76 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
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
const cors = require("cors");
var app = express();
// Get Service config and environments
const config = require("./config").getConfig();
// Connect to the database
const mongoose = require("mongoose");
mongoose
.connect(config.database.url, {
dbName: config.database.name,
})
.then(() => {
console.log("Database is connected");
})
.catch((err) => console.log(err));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(cors()); // cors for cross origin access
// Import routers
var routes = require("./routes/index");
app.use(routes);
// log all endpoints
function print(path, layer) {
if (layer.route) {
layer.route.stack.forEach(
print.bind(null, path.concat(split(layer.route.path)))
);
} else if (layer.name === "router" && layer.handle.stack) {
layer.handle.stack.forEach(
print.bind(null, path.concat(split(layer.regexp)))
);
} else if (layer.method) {
console.log(
"%s /%s",
layer.method.toUpperCase(),
path.concat(split(layer.regexp)).filter(Boolean).join("/")
);
}
}
function split(thing) {
if (typeof thing === "string") {
return thing.split("/");
} else if (thing.fast_slash) {
return "";
} else {
var match = thing
.toString()
.replace("\\/?", "")
.replace("(?=\\/|$)", "$")
.match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//);
return match
? match[1].replace(/\\(.)/g, "$1").split("/")
: "<complex:" + thing.toString() + ">";
}
}
app._router.stack.forEach(print.bind(null, []));
module.exports = app;