配图源自 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 包可以直接使用,比如 open、opn 等。
以 open 为例:
const open = require('open')
open('http://www.google.com', 'firefox')
又是一篇无营养的文章,哈哈...
The end.













网友评论