Search code examples
node.jsexpressbonjour

In Bonjour how do I hook up a web server to advertised port?


I am trying to advertise a web service on a random port and then hook up a Node/Express server to listen to that port.

I tried to implement it with the following code (ES6):

import express from 'express';
import bonjour from 'bonjour';
import http from 'http';
import getPort from 'get-port'; // to generate random port

const advertisedPort = await getPort();
const server = bonjour();
const service = server.publish({name:'MyWebServer', port: advertisedPort, type: http});

const app = express();
app.get('/', (res, req) => {
    res.send('Hello World');
});

http.createServer(app).listen(advertisedPort);

And I get an EADDRINUSE. If I use another port number in .listen(), the server comes up successfully. However, what I really want to do is to advertise a single port and then be able to listen to requests on that port.


Solution

  • False alarm folks. A reboot fixed it. Sorry to bother everybody