forked from oblador/react-native-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (52 loc) · 1.37 KB
/
index.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
import express from 'express';
import next from 'next';
import nextRoutes from 'next-routes';
import compression from 'compression';
const isProduction = process.env.NODE_ENV === 'production';
const routes = nextRoutes();
const port = parseInt(process.env.PORT, 10) || 8000;
const app = next({ dev: !isProduction });
const customHandler = routes.getRequestHandler(app);
const getAllowedSortByName = req => {
let sortBy = 'updated';
[
'recommended',
'compatibility',
'health',
'downloads',
'issues',
'stars',
].forEach(sortName => {
if (req.params.order === sortName) {
sortBy = sortName;
}
});
return sortBy;
};
app.prepare().then(() => {
const server = express();
if (isProduction) {
server.use(compression());
}
server.get('/search/:search', (req, res) => {
return app.render(req, res, '/', { search: req.params.search });
});
server.get('/:order/:topic', (req, res) => {
return app.render(req, res, '/', {
sortBy: getAllowedSortByName(req),
topic: req.params.topic,
});
});
server.get('/:order', (req, res) => {
return app.render(req, res, '/', { sortBy: getAllowedSortByName(req) });
});
server.get('*', (req, res) => {
return customHandler(req, res);
});
server.listen(port, err => {
if (err) {
throw err;
}
console.log(`Running on localhost:${port}`);
});
});