-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (52 loc) · 1.85 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
const express = require('express');
const fs = require('fs');
const https = require('https');
const subdomain = require('express-subdomain');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
require('dotenv').config(); // load local .env file into process.ENV
const db = require('./db.js');
const secret = process.env.COOKIE_SECRET;
const ports = [80, 443];
const app = express();
// SSL
const server = https.createServer({
key: fs.readFileSync('./keys/privkey.pem'),
cert: fs.readFileSync('./keys/cert.pem')
},
app
);
// allows post data to be accessed in routes
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// cookieParser and session should use the same secret
app.use(cookieParser(secret));
app.use(session({
store: new pgSession({
pg : db.pg,
conString : process.env.DATABASE_URL,
schemaName : 'tracker'
}),
rolling: false,
resave: true,
secret: secret,
saveUninitialized: false,
cookie: { secure: false, maxAge: 30 * 24 * 60 * 60 * 1000 } // 30 days
}));
server.listen(ports[1]);
app.listen(ports[0]);
// set headers for all routes
app.all('/*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://topkek.us:3000');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
next();
});
// login API route
app.use(subdomain('api', require('./api')));
app.use(subdomain('tracker', require('./tracker')));