Guy
Guy

Reputation: 13296

Opening Chrome from NodeJS while preventing duplicate tabs

In NodeJS, I am using the package Open to open a new tab on Chrome after the ExpressJS server is ready.

app.listen(port, () => console.log(`Express server is listening on port ${port}!`));
open(`http://localhost:${port}`);

However, when using Nodemon to reload the Express server on changes, I end up having multiple tabs with the same address: http://localhost:3000 as each time Open, well, opens a new tab.

Is there a way for NodeJS to open a new tab on Chrome while preventing duplications? As this is for development purposes I have full access to Chrome if any flags are needed. I can use Sockets to see if any clients are still connected but it feels there must be an easier way. Is there?

Upvotes: 0

Views: 796

Answers (1)

TinkerTenorSoftwareGuy
TinkerTenorSoftwareGuy

Reputation: 797

Does this have to be solved with Node JS code? If not, have you considered using a browser plug-in to prevent duplicate tabs? It really seems like the browser's concern. Even placing the open() command in your Node JS code seems iffy. Why not using something like this to combine them from the command line instead? That why you don't clutter your code with development-only environment concerns unless absolutely necessary.

node my-app.js && chrome

As for extensions, here are a couple from a quick search:

Let me know if that change in approach helps you out.

Upvotes: 1

Related Questions