This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devServer.js
56 lines (51 loc) · 1.55 KB
/
devServer.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
const express = require('express');
const next = require('next');
const axios = require('axios');
// eslint-disable-next-line import/no-extraneous-dependencies
const proxyMiddleware = require('http-proxy-middleware');
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV === 'development';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
if (process.env.NODE_ENV === 'development') {
server
.use(
proxyMiddleware('/api', {
target: 'https://takwimu.africa',
changeOrigin: true
})
)
.use(
proxyMiddleware('/wp-json', {
target: 'http://localhost:8080',
changeOrigin: true
})
)
.use('/flourish/:id', async (req, res) => {
axios
.get(
process.env === 'development'
? `http://localhost:8080/wp-json/hurumap-data/flourish/${req.params.id}`
: `https://takwimutech.wpengine.com/wp-json/hurumap-data/flourish/${req.params.id}`
)
.then(({ data }) => {
res.send(
data.replace(
/localhost:8080|takwimutech.wpengine.com|takwimu.africa/gi,
`http://localhost:3000`
)
);
})
.catch(() => res.sendStatus(400));
});
}
server.use(handle).listen(port, err => {
if (err) {
throw err;
}
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${port}`);
});
});