Unable to open browser window from node (hosted on heroku)

435 views Asked by At

I have a NodeJs app hosted on Heroku. As part of Authentication, I want to open a browser window when user accesses the /login route.

I am using Node's "open" library for this, and passing the URL as parameter. While this works on local machine and Node opens the browser window(with that URL), but it doesn't, when hosted on Heroku.

So, I'd like to understand if there's any limitation with Heroku platform in performing such action,and what other steps should I take to resolve this issue.

I have tried open and opener libraries.

Sample code with opener library.

  var opener = require("opener");

  url = 'https://exampleapp.herokuapp.com';
  opener(url);

  return;
1

There are 1 answers

0
Nathan On

To redirect the user to a page after a certain even within the same page you can do a simple (assuming you're using express with Nodejs):

res.redirect() or res.download() if you would like the user to be able to download an item instead.

Else, if you would like a new window to open with the new page (not overriding the previous page they were on) you will need an extra module to do so as express does not handle browser side code: I recommend Puppeteer with this example code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto(url);

  await browser.close();
})();

The {headless: false} parameter in the function puppeteer.launch() function will open a new tab in the users browser.