forked from zuhairtaha/hackyourfuture.dk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (50 loc) · 1.54 KB
/
server.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
const express = require('express')
const next = require('next')
const enforce = require('express-sslify')
const forceDomain = require('forcedomain');
const port = parseInt(process.env.PORT, 10) || 3000
// NODE_ENV is used for next.js ONLY
// NODE_ENVIRONMENT is used for custom environment checking
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
const server = express()
server.get('/robots.txt', (req, res) => (
res.status(200).sendFile('robots.txt', {
root: __dirname + '/static/',
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
}
})
));
if (process.env.NODE_ENVIRONMENT === 'production') {
server.use(enforce.HTTPS({ trustProtoHeader: true }));
server.use(forceDomain({
hostname: 'www.hackyourfuture.dk',
excludeRule: /staging/i
}));
// Firefox needs the Content-Type specified.
// For JS
server.get('*.js', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/javascript');
next();
});
// For CSS
server.get('*.css', function(req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/css');
next();
});
}
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})