forked from jsheroes/legacy-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (82 loc) · 2.43 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
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
91
92
93
94
95
96
97
98
99
100
101
const express = require('express');
const nextServer = require('next');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const sgMail = require('@sendgrid/mail');
const config = require('./config');
const dev = process.env.NODE_ENV !== 'production';
const app = nextServer({ dev, conf: config.next });
const handle = app.getRequestHandler();
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
// eslint-disable-next-line max-statements
const startServer = () => {
const server = express();
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
server.use(helmet());
server.use((req, res, next) => {
// redirect from www to non-www
const { host } = req.headers;
if (host.indexOf('www.jsheroes.io') > -1) {
res.redirect(301, `https://jsheroes.io${req.url}`);
} else {
next();
}
});
server.get('/code-of-conduct.html', (req, res) => {
res.redirect(301, '/code-of-conduct');
});
server.get('/speakers', (req, res) => {
res.redirect(301, '/');
});
server.get('/speakers/:name', (req, res) => {
const params = { name: req.params.name };
const route = '/speakers';
return app.render(req, res, route, params);
});
server.get('/workshops', (req, res) => {
res.redirect(301, '/');
});
server.get('/sw.js', (req, res) => {
res.sendFile(`${__dirname}/offline/dist/serviceWorker.js`);
});
server.get('/workshops/:name', (req, res) => {
const query = { name: req.params.name };
const route = '/workshops';
return app.render(req, res, route, query);
});
server.post('/contact-form', (req, res, next) => {
const msg = {
to: '[email protected]',
from: { email: req.body.email },
subject: 'JsHeroes ContactForm',
text: req.body.message,
};
sgMail
.send(msg)
.then(() => {
res.json({ success: true });
})
.catch(() => {
next(new Error('Email could not be sent.'));
});
});
server.get('*', (req, res) => handle(req, res));
// start
server.listen(config.port, err => {
if (err) throw err;
// eslint-disable-next-line no-console
console.log(`app is:
- running on port ${config.port}
- in ${dev ? 'DEVELOPMENT' : 'PRODUCTION'} mode
`);
});
};
app
.prepare()
.then(startServer)
.catch(ex => {
// eslint-disable-next-line no-console
console.error(ex.stack);
process.exit(1);
});