-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (53 loc) · 1.96 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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
//Our personal middleware functions
let tokenauth = require("./middleware/tokenauth.js");
let certauth = require("./middleware/certauth");
let logger = require("./middleware/logger");
const router = require("./router");
const schedule = require("node-schedule");
let tokenModel = require("./models/Tokens");
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
//Calling logger and authentication
app.use(cors())
app.use(certauth);
app.use(tokenauth);
app.use(logger);
//Allow Origin
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Expose-Headers', '*');
res.header('Timestamp', (Math.floor(Date.now())))
res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json, Token');
next();
});
app.options('*', (req, res) => {
res.setHeader("Timestamp", 0).end()
});
app.use(router);
require('./sockets/chatsockets')(io)
http.listen(process.env.PORT || 5000, () => {
if (process.env.PORT !== undefined) {
console.log("Server gestart op poort " + process.env.PORT);
} else {
console.log("Server gestart op poort 5000");
}
});
// job every 15 minute to clear out tokens.
const j = schedule.scheduleJob('* 15 * * * *', function () {
tokenModel.find({ ExpirationDate: { $lt: Date.now() } }, function (err, foundTokens) {
if (err) { console.log(err); }
foundTokens.forEach(t => t.remove())
});
});
module.exports = app;