-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
164 lines (154 loc) · 4.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//import modules
const config = require('./config/config.js');
const http = require('http');
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyPareser = require('body-parser');
const passport = require('passport');
const session = require('express-session');
const compression = require('compression');
const https = require('https');
const fs = require('fs');
const flash = require('connect-flash');
const schedule = require('node-schedule');
const cors = require('cors');
const mongodb = require('./models/common/data.js');
const MongoStore = require('connect-mongo')({ session: session });
const path = require('path');
//End import modules
var app = express();
app.use(compression());
// view engine setup
//app.set('views', config.HTTPServer.viewsRoot);
app.use(express.static(config.HTTPServer.viewsRoot));
app.use(flash());
app.use(
cors({
origin: [
'http://localhost:4200',
'http://127.0.0.1:3001',
'capacitor://localhost',
'http://localhost',
'http://localhost:8100',
],
credentials: true,
})
);
app.use(bodyPareser.urlencoded({ extended: true, limit: '50mb' }));
app.use(bodyPareser.json({ limit: '50mb' }));
app.use(cookieParser('myNtunhsCookieSecret'));
let mongoUsername = `${config.MONGODB.username}:` || '';
let mongoPassword = config.MONGODB.password || '';
let at = mongoUsername || mongoPassword ? '@' : '';
let mongoConnectionURL = `mongodb://${mongoUsername}${mongoPassword}${at}${config.MONGODB.host}:${config.MONGODB.port}/${config.MONGODB.db}?ssl=${config.MONGODB.ssl}&authSource=${config.MONGODB.authDB}`;
let sessionOptions = {
secret: 'ntunhsimsercret',
resave: false,
saveUninitialized: false,
store: new MongoStore({
url: mongoConnectionURL,
}),
cookie: {
maxAge: 60 * 60 * 24 * 1000,
//sameSite: 'none',
//secure : true
},
expires: new Date(Date.now() + 86400 * 1000),
};
if (config.HTTPServer.isProduction) {
sessionOptions.cookie.sameSite = 'none';
sessionOptions.cookie.secure = true;
}
app.use(session(sessionOptions));
app.set('trust proxy', 1);
app.use(passport.initialize());
app.use(passport.session());
app.engine('html', require('ejs').renderFile);
require('./models/users/passport.js')(passport);
require('./routes/routes.js')(app, passport);
http.createServer(app)
.on('connection', function (socket) {
socket.setTimeout(config.HTTPServer.timeout);
})
.listen(config.HTTPServer.httpPort, function () {
console.log(
'HTTP SERVER IS Listening on port :' + config.HTTPServer.httpPort
);
});
//定時清console
/*var rule = new schedule.RecurrenceRule();
rule.second =0;
rule.minute = 30;
schedule.scheduleJob(rule , ()=>
{
console.log('\033[2J');
});*/
//定時清檔案
var ruleClearFile = new schedule.RecurrenceRule();
ruleClearFile.second = 0;
ruleClearFile.minute = [0, 15, 30, 45, 55];
schedule.scheduleJob(ruleClearFile, () => {
Clear_Files();
});
//定時清產生出來的xlsx ,docx , pdf , picture
function Clear_Files() {
var paths = ['xlsx', 'docx', 'pdf', 'picture'];
for (var x = 0; x < paths.length; x++) {
fs.readdirSync(paths[x]).forEach((file) => {
fs.unlink(path.join(paths[x], file), (err) => {
if (err) throw err;
});
});
}
}
const { MongoExe } = require('./models/common/data');
async function clearExpireSessions() {
let conn = await MongoExe();
let db = conn.db('My_ntunhs');
let collection = db.collection('sessions');
collection.deleteMany(
{ expires: { $lte: new Date() } },
async function (err, docs) {
console.log('delete expire sessions', docs);
}
);
}
let scheduleDeleteExpireSession = schedule.scheduleJob(
{ rule: '0 30 0 * * *' },
async function () {
await clearExpireSessions();
}
);
/*let scheduleUpdateCourse = schedule.scheduleJob({rule: '0 30 0 * * *'} ,function () {
console.log("update course");
updateCourseMain();
});*/
//updateCourseMain();
/**
* create index of mongodb
*/
(async ()=> {
try {
let mongodbConn = await MongoExe();
let db = mongodbConn.db("My_ntunhs");
//create index for stored history score
//為已上傳的歷史成績做索引
let storedHistoryScoreCollection = db.collection("storedHistoryScore");
await storedHistoryScoreCollection.createIndex({
courseName: 1
});
console.log("create 'courseName' index for stored history score successful");
await storedHistoryScoreCollection.createIndex({
courseTeacher: 1
});
console.log("create 'courseTeacher' index for stored history score successful");
//為"學生資訊"做索引
let studentCollection = db.collection("Students");
await studentCollection.createIndex({
username: 1
});
} catch(e) {
console.error(e);
process.exit(1);
}
})();