I am trying to set the target show in my proxy options to have the same port that is coming from my bonjour service. Also if the port change I would like to run the proxy function to setting the target. any help please. The code works however the issue is that the proxy only sets once. If the value from servicePort change the proxy does not update it continues to run with the old target
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const { WebMidi } = require("webmidi");
const { createProxyMiddleware } = require("http-proxy-middleware");
var cors = require("cors");
var bonjour = require("bonjour")();
const app = express();
const httpServer = createServer(app);
let servicePort = bonjour.find({ type: "gametime" }, function (service) {
console.log("Found an HTTP server:", service);
return service.port
});
// proxy middleware options
const options = {
target: `http://localhost:63695${servicePort}`, // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
};
// create the proxy (without context)
const proxyService = createProxyMiddleware(options);
// set cors
app.use(cors());
// route: forward all request to proxy service
app.use("/", proxyService);
httpServer.listen(9080);