forked from llooker/pbl-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (72 loc) · 2.93 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const session = require('express-session');
const pg = require('pg');
const pgSession = require('connect-pg-simple')(session);
//how can we make this dynamic???
//Nick thinks we sholdn't have to use dotenv
//for node file our env variables should be read automatically b/c of the source command when starting app
require('dotenv').config();
// require('dotenv').config({ path: ".env.atom" });
// require('dotenv').config({ path: ".env.vision" });
// console.log('NODE_ENV', process.env.NODE_ENV)
// console.log('HOST', process.env.HOST)
// console.log('LOOKERSDK_BASE_URL', process.env.LOOKERSDK_BASE_URL)
// console.log('PACKAGE_NAME', process.env.PACKAGE_NAME)
let pgPool;
if (process.env.NODE_ENV === 'production') {
pgPool = new pg.Pool({
// Insert pool options here
database: process.env.POSTGRES_DATABASE_NAME,
user: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
host: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
});
} else {
pgPool = new pg.Pool({
// Insert pool options here
database: process.env.POSTGRES_DATABASE_NAME,
user: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
port: 5432,
host: process.env.POSTGRES_IP_ADDRESS,
});
}
const sess = {
secret: 'keyboard catv1.2',
resave: true,
cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, // 30 days,
store: new pgSession({
pool: pgPool, // Connection pool
tableName: 'session' // Use another table-name than the default "session" one
}),
}
if (app.get('env') === 'production') {
app.set('trust proxy', 1) // trust first proxy
sess.cookie.secure = true // serve secure cookies
}
app.use(session(sess))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb', extended: true }));
const port = process.env.PORT || 5000;
let routes = require('./routes/index')
app.use('/', routes)
//https://medium.com/@paulrohan/deploying-a-react-node-mongodb-app-to-google-cloud-platforms-google-app-engine-1ba680447d59
if (process.env.NODE_ENV === 'production') {
//https://stackoverflow.com/questions/41944776/force-ssl-on-app-engine-flexible-environment-custom-runtime/48085977#48085977
//5 upvoted asnswer
app.use(function (req, res, next) {
if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] === "http") {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
next();
});
app.use(express.static(path.join(__dirname, `client/${process.env.PACKAGE_NAME}/build`)))
// Handle React routing, return all requests to React app
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, `client/${process.env.PACKAGE_NAME}/build`, 'index.html'));
});
}
app.listen(port, () => console.log(`Example app listening on port ${port}!`))