This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathweb.js
90 lines (74 loc) · 2.08 KB
/
web.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
89
90
var config = require('./config').config,
express = require('express'),
morgan = require('morgan'),
http = require('http'),
path = require('path'),
engine = require('ejs-locals'),
helmet = require('helmet'),
csp = require('helmet-csp'),
routes = require('./routes/index.js'),
app = express();
app.use(helmet.xssFilter());
app.use(helmet());
app.use(morgan('common'));
app.set('port', config.port);
app.use(express.compress());
//use ejs-locals for all ejs templates:
app.engine('ejs', engine);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(function(req, res, next){
if(process.env.ENV == 'production'){
if (req.headers['x-forwarded-proto'] == 'https') {
next();
} else {
res.redirect('https://' + req.headers.host + req.path);
}
}else{
next();
}
});
app.use(app.router);
app.use(function(req, res, next){
throw new Error(req.url + ' not found');
});
app.use(function(err, req, res, next){
res.status(404);
routes.index(req, res);
//api.sendResponse(res, 404);
});
//Base route
app.get('/', routes.index);
app.get('/:id', routes.index);
app.get('/home', routes.index);
app.get('/browser', routes.index);
app.get('/browser/:id', routes.index);
app.get('/browser/user/:id', routes.index);
app.get('/join/:url', routes.index);
app.get('/login/facebook', function(req, res){
res.redirect(config.api_url + '/auth/facebook');
});
app.get('/login/twitter', function(req, res){
res.redirect(config.api_url + '/auth/twitter');
});
app.get('/login/logout', function(req, res){
res.redirect(config.api_url + '/auth/logout');
});
//Global route handler
app.all('/*', routes.global);
//Global route handlers (method specific)
app.get('/*', routes.globalGet);
app.post('/*', routes.globalPost);
app.put('/*', routes.globalPut);
app.delete('/*', routes.globalDelete);
var server = app.listen(app.get('port'), function(){
console.log('Listening on port %d', server.address().port);
});
process.on('message', function(msg) {
if (msg == 'shutdown') {
console.log('Gracefully shutting down app');
server.close();
process.exit(0);
}
});