美文网首页
Node.js 实现浏览器打开链接

Node.js 实现浏览器打开链接

作者: 越前君 | 来源:发表于2022-02-08 18:54 被阅读0次
配图源自 Freepik

在 Node.js 中实现在浏览器中打开指定 URL。

利用 Node.js 提供的 child_process.exec() 方法即可,但是不同操作系统下,指令有所不同:

const { exec } = require('child_process')
const uri = 'https://www.google.com'

// Windows
exec('start https://www.google.com')

// Mac
exec('open https://www.google.com')

// Otherwise: https://portland.freedesktop.org/doc/xdg-open.html
exec('xdg-open https://www.google.com')

社区上有一些 NPM 包可以直接使用,比如 openopn 等。

open 为例:

const open = require('open')

open('http://www.google.com', 'firefox')

又是一篇无营养的文章,哈哈...

The end.

相关文章

网友评论

      本文标题:Node.js 实现浏览器打开链接

      本文链接:https://www.haomeiwen.com/subject/ptylqrtx.html