Skip to content

Instantly share code, notes, and snippets.

@peterreisz
Last active May 30, 2024 10:00
Show Gist options
  • Save peterreisz/96593915c407de2d139120ade96c9be0 to your computer and use it in GitHub Desktop.
Save peterreisz/96593915c407de2d139120ade96c9be0 to your computer and use it in GitHub Desktop.
Dev server proxy for stencil. Backend is running on port 3000, stencil on 3333, open port 5000 for development. Only `/api` requests are sent to the backend, everything else served by stencil.
const http = require('http');
const httpProxy = require('http-proxy');
const proxyApi = new httpProxy.createProxyServer({
target: {
host: '127.0.0.1',
port: 3000
}
});
proxyApi.on('error', () => {});
const proxyStencil = new httpProxy.createProxyServer({
target: {
host: '127.0.0.1',
port: 3333
}
});
proxyStencil.on('error', () => {});
const proxyServer = http.createServer((req, res) => {
if (req.url.match(/\/api/)) {
proxyApi.web(req, res);
} else {
proxyStencil.web(req, res);
}
});
proxyServer.on('error', () => {});
proxyServer.on('upgrade', (req, socket, head) => {
proxyStencil.ws(req, socket, head);
});
console.log('Listen: http://127.0.0.1:5000');
proxyServer.listen(5000);
{
"name": "stencil-dev-proxy",
"version": "0.1.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"license": "MIT",
"devDependencies": {
"http-proxy": "^1.17.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment