-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_server.mjs
46 lines (30 loc) · 992 Bytes
/
dev_server.mjs
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
import express from 'express'
import fetch from 'node-fetch'
import urlJoin from 'url-join'
import nocache from 'nocache'
let server = express()
server.use(nocache())
if(process.env.OXO_DEV_BACKEND_PROXY_URL === undefined) {
throw new Error('please set OXO_DEV_BACKEND_PROXY_URL before running dev server')
}
server.use(/^\/api.*/, async (req, res) => {
let backendUrl = urlJoin(process.env.OXO_DEV_BACKEND_PROXY_URL, req.originalUrl)
console.log('forwarding api request to: ' + backendUrl)
try {
let apiResponse = await fetch(backendUrl, {
redirect: 'follow',
method: req.method,
body: req.body
})
res.header('content-type', apiResponse.headers.get('content-type'))
res.status(apiResponse.status)
apiResponse.body.pipe(res)
} catch(e) {
console.log(e)
}
})
server.use(express.static('dist'))
server.get(/^(?!\/api).*$/, (req, res) => {
res.sendFile(process.cwd() + '/dist/index.html')
})
server.listen(3000)