Created
August 23, 2019 01:49
-
-
Save tcrowe/c9a11fca78447ba78c5de2e7b9ba10d2 to your computer and use it in GitHub Desktop.
polka sapper api proxy example
This file contains 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
/* | |
they usually use ES Modules but i used CommonJS here | |
you can port it to suite your need | |
*/ | |
const sirv = require("sirv"); | |
const polka = require("polka"); | |
const compression = require("compression"); | |
const sapper = require("@sapper/server"); | |
const proxy = require("express-http-proxy"); | |
const { PORT, NODE_ENV } = process.env; | |
const dev = NODE_ENV === "development"; | |
const apiProxy = proxy("http://127.0.0.1:8930"); | |
// assign server variable | |
const server = polka(); | |
// ⚠️ hack to make the proxy work with polka | |
// express-http-proxy is expecting these methods | |
server.use(function(req, res, next) { | |
res.status = code => (res.statusCode = code); | |
res.set = res.setHeader.bind(res); | |
next(); | |
}); | |
// proxy to api | |
server.use("/api", apiProxy); | |
// do normal sapper server stuff | |
server | |
.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