Create s.js
Browse files
s.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const { createProxyMiddleware } = require("http-proxy-middleware");
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
|
| 6 |
+
app.use("/s:port", (req, res, next) => {
|
| 7 |
+
const port = req.params.port;
|
| 8 |
+
|
| 9 |
+
if (!/^\d+$/.test(port)) {
|
| 10 |
+
return res.status(400).send("Porta inválida");
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
const target = `http://localhost:${port}`;
|
| 14 |
+
console.log(`Proxying ${req.originalUrl} -> ${target}`);
|
| 15 |
+
|
| 16 |
+
return createProxyMiddleware({
|
| 17 |
+
target,
|
| 18 |
+
changeOrigin: true,
|
| 19 |
+
pathRewrite: {
|
| 20 |
+
[`^/s${port}`]: ""
|
| 21 |
+
}
|
| 22 |
+
})(req, res, next);
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
app.get("/", (req, res) => {
|
| 26 |
+
res.send("Proxy dinâmico ativo! Use /sPORTA (ex: /s8080).");
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
app.listen(7860, () => {
|
| 30 |
+
console.log("Servidor proxy rodando em http://localhost:7860");
|
| 31 |
+
});
|