-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
80 lines (70 loc) · 2.17 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
const express = require('express');
const next = require('next');
const chalk = require('chalk');
const dev = process.env.NODE_ENV !== 'production';
const port = process.env.PORT || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();
const logger = console.log;
if (dev) {
logger(chalk.cyan('Starting the development server...\n'));
} else {
logger(chalk.cyan('Starting the production server...\n'));
}
app.prepare()
.then(() => {
const server = express();
server.get('/r/:subredditName/comments/:postId/:safeTitleURL?', (req, res) => {
const actualPage = '/';
const queryParams = {
...req.query,
...req.params,
path: '/r/:subredditName/comments/:postId/:safeTitleURL',
url: req.url,
};
app.render(req, res, actualPage, { ...queryParams } );
});
server.get('/r/:subredditName/:sort(best|hot|new|rising|controversial|top|gilded|all|popular|random)?', (req, res) => {
const actualPage = '/';
const queryParams = {
...req.query,
...req.params,
path: `/r/:subredditName/:sort(${req.param.sort})`,
url: req.url,
};
app.render(req, res, actualPage, { ...queryParams } );
});
server.get('/r/:subredditName', (req, res) => {
const actualPage = '/';
const queryParams = {
...req.query,
...req.params,
path: `/r/:subredditName`,
url: req.url,
};
app.render(req, res, actualPage, { ...queryParams } );
});
server.get('/:sort(best|hot|new|rising|controversial|top|gilded|all|popular|random)?', (req, res) => {
const actualPage = '/';
const queryParams = {
...req.query,
...req.params,
path: `/:sort(${req.params.sort})`,
url: req.url,
};
app.render(req, res, actualPage, { ...queryParams } );
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) {
throw chalk.red(`An error occured: ${err.message}`);
}
logger(chalk.green('Server is ready!'));
});
})
.catch((ex) => {
logger(chalk.red(ex.stack));
process.exit(1);
});