-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
81 lines (72 loc) · 1.72 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
/**
* Module dependencies
*/
var
express = require('express'),
app = express(),
helmet = require('helmet'),
shrinkRay = require('shrink-ray-current'),
connect = require('connect-livereload'),
config = require('./config');
/**
* Compression
*/
app.use(shrinkRay());
/**
* Secure Express
*/
app.use(helmet());
/**
* Livereload if develop
*/
if (process.develop) {
app.use(connect({ port: 35729 }));
}
/**
* View dir
*/
app.use(express.static(__dirname + '/public'));
/**
* View engine setup
*/
app.set('view engine', 'pug');
/**
* Handle 404
*/
app.get('*', function(req, res){
res.status(404);
res.render('404');
});
/**
* Create HTTPS server and Listen on provided port
*/
if (process.production) {
var http2 = require('spdy'),
options = {
// The path to SSL certificate
// cert: fs.readFileSync('../fullchain.pem'),
// key: fs.readFileSync('../privkey.pem')
};
http2.createServer(options, app).listen(443);
// Redirect HTTP to HTTPS if production
app.use(function(req, res, next) {
if (req.secure && req.headers.host.match(/^www/) === null) {
next();
} else {
res.redirect(301, 'https://' + req.headers.host + req.url);
}
// if (req.headers.host.match(/^www/) !== null ) {
// res.redirect(301, 'https://' + req.headers.host.replace(/^www\./, '') + req.url);
// } else {
// next();
// }
});
}
/**
* Create HTTP server and Listen on provided port
*/
var http = require('http');
http.createServer(app).listen(process.host, function (err) {
if (err) throw err;
console.log('Server start on ' + process.host + ' port.');
});