🚧 working proxy
This commit is contained in:
parent
d267676728
commit
9e1aae22d8
1 changed files with 35 additions and 18 deletions
|
|
@ -1,33 +1,50 @@
|
|||
import cors from "cors";
|
||||
import express from "express";
|
||||
import { createProxyMiddleware } from "http-proxy-middleware";
|
||||
import http from "http";
|
||||
|
||||
const PROXY_PORT = 65433;
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
|
||||
app.use("/", (req, res, next) => {
|
||||
// Extract the target from the request URL
|
||||
// const { protocol, host } = url.parse(req.url);
|
||||
// const target = `${protocol}//${host}`;
|
||||
const target = "http://localhost:11434";
|
||||
|
||||
// Create a new proxy middleware for this request
|
||||
const proxy = createProxyMiddleware({
|
||||
target,
|
||||
changeOrigin: true,
|
||||
ws: true,
|
||||
pathRewrite: {
|
||||
"^/": "/api/generate",
|
||||
app.use((req, res, next) => {
|
||||
// Proxy the request
|
||||
const { origin, ...headers } = req.headers;
|
||||
const proxy = http.request(
|
||||
req.headers["x-continue-url"] as string,
|
||||
{
|
||||
method: req.method,
|
||||
headers: headers,
|
||||
},
|
||||
});
|
||||
(response) => {
|
||||
// Pipe the response stream directly to the client
|
||||
response.pipe(res);
|
||||
}
|
||||
);
|
||||
|
||||
// Call the middleware
|
||||
proxy(req, res, next);
|
||||
// Pipe the request stream directly to the proxy
|
||||
req.pipe(proxy);
|
||||
});
|
||||
|
||||
// http-middleware-proxy
|
||||
// app.use("/", (req, res, next) => {
|
||||
// // Extract the target from the request URL
|
||||
// const target = req.headers["x-continue-url"] as string;
|
||||
// const { origin, ...headers } = req.headers;
|
||||
|
||||
// // Create a new proxy middleware for this request
|
||||
// const proxy = createProxyMiddleware({
|
||||
// target,
|
||||
// ws: true,
|
||||
// headers: {
|
||||
// origin: "",
|
||||
// },
|
||||
// });
|
||||
|
||||
// // Call the middleware
|
||||
// proxy(req, res, next);
|
||||
// });
|
||||
|
||||
export function startProxy() {
|
||||
console.log("Starting proxy");
|
||||
app.listen(PROXY_PORT, () => {
|
||||
console.log(`Proxy server is running on port ${PROXY_PORT}`);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue