Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

61
Views
node js proxy service issue updating port

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);
7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.