forked from syntaxfm/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·47 lines (40 loc) · 1.2 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
const express = require('express');
const next = require('next');
const Router = require('./routes').Router;
const dev = process.env.NODE_ENV !== 'production';
const port = parseInt(process.env.PORT, 10) || 80;
const app = next({ dev });
const handle = app.getRequestHandler();
const { getShows, getShow, getAllShowSickPicks } = require('./lib/getShows');
app.prepare().then(() => {
const server = express();
// API endpoints
server.get('/api/shows', (req, res) => {
res.json(getShows());
});
server.get('/api/shows/:number', (req, res) => {
const show = getShow(req.params.number);
if (show) {
res.json(show);
return;
}
res.status(404).json({ message: 'Sorry not found' });
});
server.get('/api/sickpicks', (req, res) => {
res.json(getAllShowSickPicks());
});
// Custom Next.js URLs
Router.forEachPrettyPattern((page, pattern, defaultParams) => {
server.get(pattern, (req, res) => {
app.render(
req,
res,
`/${page}`,
Object.assign({}, defaultParams, req.query, req.params)
);
});
});
// everything else
server.get('*', (req, res) => handle(req, res));
server.listen(port, () => `Listening on ${port}`);
});