forked from react-native-community/directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (90 loc) · 2.52 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
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
102
103
104
105
106
107
import express from 'express';
import next from 'next';
import cors from 'cors';
import nextRoutes from 'next-routes';
import bodyParser from 'body-parser';
import compression from 'compression';
import * as Sorting from './common/sorting';
import { handleFilterLibraries } from './common/search';
import data from './build/data.json';
const originalData = [...data.libraries];
const sortedData = {
updated: Sorting.updated([...originalData]),
recommended: Sorting.recommended([...originalData]),
compatibility: Sorting.compatibility([...originalData]),
quality: Sorting.quality([...originalData]),
downloads: Sorting.downloads([...originalData]),
issues: Sorting.issues([...originalData]),
stars: Sorting.stars([...originalData]),
};
const isProduction = process.env.NODE_ENV === 'production';
const routes = nextRoutes();
const port = parseInt(process.env.PORT, 10) || 8000;
const app = next({ dev: !isProduction, quiet: true });
const customHandler = routes.getRequestHandler(app);
const getAllowedOrderString = req => {
let sortBy = 'updated';
['recommended', 'compatibility', 'quality', 'downloads', 'issues', 'stars'].forEach(sortName => {
if (req.params.order === sortName) {
sortBy = sortName;
}
});
return sortBy;
};
const processRequest = (req, res) => {
const sortBy = getAllowedOrderString(req);
const libraries = sortedData[sortBy];
if (req.query.json) {
return res.status(200).json(
handleFilterLibraries({
libraries,
queryTopic: req.params.topic,
querySearch: req.query.search,
support: {
ios: req.query.ios,
android: req.query.android,
expo: req.query.expo,
web: req.query.web,
},
})
);
}
return app.render(req, res, '/', {
libraries,
sortBy,
topic: req.params.topic,
search: req.query.search,
support: {
ios: req.query.ios,
android: req.query.android,
expo: req.query.expo,
web: req.query.web,
},
});
};
app.prepare().then(() => {
const server = express();
server.use('/static', express.static('static'));
server.use(
bodyParser.urlencoded({
extended: false,
})
);
server.use(
cors({
origin: '*',
})
);
if (isProduction) {
server.use(compression());
}
server.get('/:order/:topic', processRequest);
server.get('/:order', processRequest);
server.get('*', processRequest);
server.listen(port, err => {
if (err) {
throw err;
}
console.log(`Running on localhost:${port}`);
});
});