-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
134 lines (94 loc) · 3.59 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
125
126
127
128
129
130
131
132
133
134
const env = require("./env");
const { config } = require("./helper");
const app = require("express")();
const controller = require("./controller");
const compression = require("compression");
const { json } = require("body-parser");
const { isMaster } = require("cluster");
app.disable("x-powered-by");
/* Enforce SSL
-----------------------------------------------*/
if(config.force_ssl) {
app.use(function(req, res, next){
if(req.secure ||
(req.protocol === "https") ||
(req.get("x-forwarded-proto") === "https")){
next();
}
else{
res.redirect("https://" + req.headers.host + req.url);
}
});
}
/* Compression
-----------------------------------------------*/
app.use(compression({
filter: function(req, res){
return req.headers["x-no-compression"] ?
false
:
compression.filter(req, res);
}
}));
/* CORS
-----------------------------------------------*/
app.use(function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//req.env = env;
next();
});
/* Body Parser
-----------------------------------------------*/
app.use(json());
/* Routes
-----------------------------------------------*/
app.get("/", controller.index);
app.post("/add", controller.add_bulk);
app.post("/add/:id/:content", controller.add);
app.post("/update", controller.update_bulk);
app.post("/update/:id/:content", controller.update);
app.get("/search", controller.search);
app.get("/search/:query", controller.search);
app.post("/remove", controller.remove_bulk);
app.post("/remove/:id", controller.remove);
if(isMaster) console.info("\x1b[32m\x1b[1m",
"\n" +
" _____ _ ____ _ _ \n" +
"| ___| | _____ __/ ___| ___ __ _ _ __ ___| |__ (_)___ \n" +
"| |_ | |/ _ \\ \\/ /\\___ \\ / _ \\/ _` | '__/ __| '_ \\ | / __|\n" +
"| _| | | __/> < ___) | __/ (_| | | | (__| | | |_ | \\__ \\\n" +
"|_| |_|\\___/_/\\_\\|____/ \\___|\\__,_|_| \\___|_| |_(_)/ |___/\n" +
" |__/ \n" +
"Version: " + require("./package.json").version + "\n" +
"-------------------------------------------------------------", "\x1b[0m"
);
/* Start HTTPS Server
-----------------------------------------------*/
if(config.https || config.force_ssl){
const { readFileSync, existsSync } = require("fs");
if(existsSync("../../cert/" + env + ".pem") && existsSync("../../cert/" + env + ".crt")){
const {createServer} = require("https");
createServer({
key: readFileSync("../../cert/" + env + ".pem", "utf8"),
cert: readFileSync("../../cert/" + env + ".crt", "utf8")
}, app).listen(config.port_ssl, function(err){
if(err) throw err;
console.info("Server@" + process.pid + " listening on https://localhost" + (config.port_ssl !== 443 ? ":" + config.port_ssl : ""));
});
}
else{
console.info("Could not find certificates located at: /certs. Therefore HTTPS server can't initialize.");
}
}
/* Start HTTP Server
-----------------------------------------------*/
if(!config.force_ssl){
const server = app.listen(config.port, function(err){
if(err) throw err;
console.info("Server@" + process.pid + " listening on http://localhost" + (config.port !== 80 ? ":" + config.port : ""));
});
if(process.env.NODE_ENV === "test"){
module.exports = server;
}
}