简体   繁体   English

无法从节点(托管在 heroku 上)打开浏览器 window

[英]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.

相关问题 无法将Heroku上托管的Node应用程序与MongoDB Cluster连接 - Unable to connect Node app hosted on Heroku with MongoDB Cluster 本地主机反应应用程序未从 Heroku 托管节点 API 接收 cookie - Localhost react app not receiving cookie from Heroku hosted node API 如何从托管在不同环境中的文件执行由 Heroku 托管的 node.js 文件中的函数? - How can I execute functions in a node.js file hosted by Heroku from a file hosted in a different environment? 无法在 heroku 上打开应用程序。 反应,node.js - Unable to open app on heroku. React, node.js 我可以用 Node.JS 打开非浏览器 window 吗? - Can I open a non-browser window with Node.JS? 从节点打开Internet Explorer浏览器 - Open Internet explorer browser from node 无法在Heroku中安装节点 - Unable to install node in Heroku Node-Inspector无法打开Chrome浏览器并抛出错误ENOENT - Node-Inspector unable to open chrome browser throwing Error ENOENT 无法从 MERN 应用程序的后端在浏览器中接收/设置 cookies,后端托管在 heroku 上,前端托管在 netlify - Not able to receive/set cookies in browser from backend in MERN app with backend hosted on heroku and frontend on netlify 如何从子域的 Heroku 中托管的 Angular+Node 应用程序重定向到 HTTPS? - How to redirect to HTTPS from Angular+Node app hosted in Heroku from a subdomain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM