-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (34 loc) · 877 Bytes
/
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
import 'dotenv/config';
import jsonServer from 'json-server';
const server = jsonServer.create();
const router = jsonServer.router('build/db.json');
const middlewares = jsonServer.defaults();
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);
// Access Control
server.use((req, res, next) => {
const isAuthorized = (req) =>
req.headers.authorization === process.env.API_KEY;
if (isAuthorized(req)) {
next();
} else {
res.sendStatus(401);
}
});
// Add custom routes before JSON Server router
server.get('/v1/search', (req, res) => {
res.jsonp(req.query);
});
// Customize response
router.render = (req, res) => {
res.jsonp({
meta: {
status: 200,
},
addresses: res.locals.data,
});
};
server.use('/v1/search', router);
server.listen(3001, () => {
console.log('JSON Server is running');
});