[英]Unable to open browser window from node (hosted on heroku)
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.我在 Heroku 上托管了一个 NodeJs 应用程序。作为身份验证的一部分,我想在用户访问 /login 路由时打开浏览器 window。
I am using Node's "open" library for this, and passing the URL as parameter.我为此使用 Node 的“开放”库,并将 URL 作为参数传递。 While this works on local machine and Node opens the browser window(with that URL), but it doesn't, when hosted on Heroku.
虽然这适用于本地计算机并且 Node 打开浏览器窗口(使用该 URL),但是当托管在 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.所以,我想了解 Heroku 平台在执行此类操作时是否有任何限制,以及我应该采取哪些其他步骤来解决此问题。
I have tried open and opener libraries.我已经尝试过开放式和开放式图书馆。
Sample code with opener library.带有 opener 库的示例代码。
var opener = require("opener");
url = 'https://exampleapp.herokuapp.com';
opener(url);
return;
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):要在某个页面后将用户重定向到某个页面,即使在同一页面内,您也可以做一个简单的操作(假设您使用 Express 和 Nodejs):
res.redirect()
or res.download()
if you would like the user to be able to download an item instead. res.redirect()
或res.download()
如果您希望用户能够下载项目。
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:否则,如果你想用新页面打开一个新的 window(不覆盖它们所在的上一页),你将需要一个额外的模块来执行此操作,因为 express 不处理浏览器端代码:我建议使用此示例代码的Puppeteer :
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. function puppeteer.launch
puppeteer.launch()
function 中的{headless: false}
参数将在用户浏览器中打开一个新选项卡。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.