Last active
May 8, 2020 23:23
-
-
Save fakiolinho/4a63dcce2a046153d1bd76e5569d9250 to your computer and use it in GitHub Desktop.
Proxy an xhr request by using sapper, polka and http-proxy-middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sirv from 'sirv'; | |
import polka from 'polka'; | |
import compression from 'compression'; | |
import { createProxyMiddleware } from 'http-proxy-middleware'; | |
import * as sapper from '@sapper/server'; | |
const { PORT, NODE_ENV } = process.env; | |
const dev = NODE_ENV === 'development'; | |
const server = polka(); | |
// Request is sent to /api/* | |
server.use("/api", createProxyMiddleware({ | |
target: 'https://example.com', | |
changeOrigin: true, | |
pathRewrite: { | |
'^/api': '/', // remove base path since we need to drop "api" from the path | |
}, | |
})); | |
server // You can also use Express | |
.use( | |
compression({ threshold: 0 }), | |
sirv('static', { dev }), | |
sapper.middleware() | |
) | |
.listen(PORT, err => { | |
if (err) console.log('error', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment