forked from Chinlinlee/Burni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (90 loc) · 2.73 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
const express = require('express');
const bodyParser = require('body-parser');
const http = require('http');
const compress = require('compression');
const { handleError } = require('./models/FHIR/httpMessage');
//login
const cookieParser = require('cookie-parser');
const session = require('express-session');
const flash = require('connect-flash');
const mongodb = require('./models/mongodb');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')({
session: session
});
//
require('dotenv').config();
const port = process.env.SERVER_PORT;
const app = express();
require('rootpath')();
require('dotenv').config();
app.use(compress());
app.use(flash());
app.use(express.static('public'));
app.use(express.urlencoded({
extended: true
}));
app.use(express.json({
"limit": "50mb"
}));
app.use(express.json({
"type": "application/fhir+json",
"limit": "50mb"
}));
app.use(express.text({
"type": ["text/*", "/_xml", "xml", "+xml"]
}));
app.use((err, req, res, next) => {
// This check makes sure this is a JSON parsing issue, but it might be
// coming from any middleware, not just body-parser:
// if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
// console.error(err);
// return res.sendStatus(400); // Bad request
// }
if (err) {
console.error(err);
return res.status(400).send(handleError.processing(err));
}
next();
});
app.use(cookieParser());
//login
app.use(session({
secret: process.env.SERVER_SESSION_SECRET_KEY || 'secretKey',
resave: true,
saveUninitialized: true,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
cookie: {
httpOnly: true,
maxAge: 60 * 60 * 1000
}
}));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept ,Authorization");
res.header("Vary", "Origin");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
//login
require("routes.js")(app);
app.engine('html', require('ejs').renderFile);
//
http.createServer(app).listen(port, function() {
console.log(`http server is listening on port:${port}`);
});
// Require(cache) validator when bootstrap
if (process.env.ENABLE_VALIDATOR === "true") {
let waitDbConnection = setInterval(() => {
if (mongoose.connection.readyState === 1) {
require("./utils/validator").validator;
clearInterval(waitDbConnection);
} else {
console.log("validator is waiting for DB connecting...");
}
}, 250);
}
module.exports = app;