-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (65 loc) · 1.68 KB
/
server.js
File metadata and controls
75 lines (65 loc) · 1.68 KB
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
const express = require("express");
const mongoose = require("mongoose");
const compression = require("compression");
const { resolve } = require("path");
const dotenv = require("dotenv");
const { articles, article } = require("./source/routes");
process.on("uncaughtException", (error, origin) => {
// eslint-disable-next-line no-console
console.log(`Caught exception: ${error}
Exception origin: ${origin}`);
});
const staticPath = resolve(__dirname, "public");
const ENV_PATH = resolve(__dirname, ".env");
const envConfig = dotenv.config({
allowEmptyValues: false,
path: ENV_PATH
});
if (envConfig.error) {
throw envConfig.error;
} else {
Object.entries(envConfig).forEach(([name, value]) => {
process.env[name] = value;
});
}
const { MONGODB_PORT, MONGODB_HOST, MONGODB_NAME } = process.env;
const PORT = process.env.PORT || 3000;
const server = express();
server.disable("x-powered-by");
server.use(compression());
/**
* Statics
*/
server.get(
["/favicon.ico", "/styles/*", "/images/*"],
express.static(staticPath)
);
/**
* Statics for article
*/
server.get("/:reponame", express.static("/websites/articles/"));
/**
* Build static for article
*/
server.get("/:reponame", async (req, res, next) => {
await article.notFound(req, res, next);
});
/**
* Show articles list
* @return {string} - html of the page
*/
server.get("/", articles.get);
(async () => {
mongoose.Promise = global.Promise;
await mongoose.connect(
`mongodb://${MONGODB_HOST}:${MONGODB_PORT}/${MONGODB_NAME}`,
{
useUnifiedTopology: true,
useNewUrlParser: true,
useCreateIndex: true
}
);
server.listen(PORT, () => {
console.log(`Server listen on ${PORT}`);
});
})();