-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (73 loc) · 2.36 KB
/
Copy pathapp.js
File metadata and controls
87 lines (73 loc) · 2.36 KB
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
'use strict';
const path = require('path');
const express = require('express');
const session = require('express-session');
const MemcachedStore = require('connect-memjs')(session);
const passport = require('passport');
const config = require('./config');
const app = express();
// Load View Engine
app.disable('etag');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(__dirname+ '/public'));
app.set('view engine', 'pug');
app.set('trust proxy', true);
// [START session] -> found from google cloud tutorials
// Configure the session and session storage.
const sessionConfig = {
resave: true,
rolling: true,
saveUninitialized: false,
secret: config.get('SECRET'),
signed: true,
cookie: {
// Session expires after 30 minutes of inactivity
expires: 30 * 1000 * 60
}
};
// In production use the Memcache instance to store session data,
// otherwise fallback to the default MemoryStore in development.
if (config.get('NODE_ENV') === 'production' && config.get('MEMCACHE_URL')) {
if (config.get('MEMCACHE_USERNAME') && (config.get('MEMCACHE_PASSWORD'))) {
sessionConfig.store = new MemcachedStore({
servers: [config.get('MEMCACHE_URL')],
username: config.get('MEMCACHE_USERNAME'),
password: config.get('MEMCACHE_PASSWORD')});
} else {
sessionConfig.store = new MemcachedStore({
servers: [config.get('MEMCACHE_URL')]
});
}
}
app.use(session(sessionConfig));
// [END session]
// OAuth2
app.use(passport.initialize());
app.use(passport.session());
app.use(require('./lib/oauth2').router);
// Books
app.use('/users', require('./users/crud'));
// Home Route
app.get('/', (req, res) => {
res.redirect('/users');
});
// Basic 404 handler -> found from google cloud tutorials
app.use((req, res) => {
res.status(404).send('Not Found');
});
// Basic error handler -> found from google cloud tutorials
app.use((err, req, res, next) => {
console.error(err);
// If our routes specified a specific response, then send that. Otherwise,
// send a generic message so as not to leak anything.
res.status(500).send(err.response || 'Something broke!');
});
if (module === require.main) {
// [START server]
const server = app.listen(config.get('PORT'), () => {
const port = server.address().port;
console.log(`App listening on port ${port}`);
});
// [END server]
}
module.exports = app;