-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 1.67 KB
/
index.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
var express = require('express');
var serveStatic = require('serve-static');
var bodyParser = require('body-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var r = require('rethinkdb');
var endex = require('endex');
module.exports = function appctor(cfg) {
var mainDomain = cfg.env.CANONICAL_HOST || 'www.parkedproject.com';
var conn;
r.connect(cfg.rethinkdb).then(function (connection) {
conn = connection;
return endex.db('parkedproject')
.run(conn);
});
function requireLogin(req, res, next) {
if (req.session.username) return next();
else return res.redirect('/');
}
var app = express();
app.set('trust proxy',true);
app.use(function yesCanonicalWww(req, res, next) {
if (mainDomain.slice(0,4) == 'www.' &&
req.hostname == mainDomain.slice(4)) {
return res.redirect(301,
req.protocol + '://' + mainDomain + req.originalUrl);
} else return next();
});
app.use(serveStatic(__dirname + '/static'));
app.use(session({
store: new RedisStore({
host: cfg.redis.hostname,
port: cfg.redis.port}),
secret: cfg.env.SECRET_KEY_BASE,
resave: false,
saveUninitialized: false
}));
app.use(function (req, res, next) {
if (!req.session) {
return next(new Error("couldn't find sessions"));
}
else return next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
if (req.hostname == mainDomain)
return res.render('index.jade');
else return res.render('parking.jade',{
title: req.hostname.replace(/^www\./,''),
headline: 'Coming soon'
});
});
return app;
};