Answers:
使用opn,因为它将处理跨平台问题。安装:
$ npm install opn
使用方法:
var opn = require('opn');
// opens the url in the default browser
opn('http://sindresorhus.com');
// specify the app to open in
opn('http://sindresorhus.com', {app: 'firefox'});
opn而不是open现在不建议使用后者。
var url = 'http://localhost';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);
&URL中的应当使用^&
您可能需要使用...的值实现开关
require('os').type()
然后使用spawn("open")还是spawn("xdg-open")取决于平台?
require('child_process').spawn('explorer', ['url'])是一个很好的单人!
恕我直言,最简单,最整洁的方法是使用一个名为openurl的npm包。做一个npm install openurl。您可以在Nodejs REPL中快速尝试这种方法
require("openurl").open("http://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url")
如果需要,您也可以发送电子邮件;
require("openurl").open("mailto:janedoe@example.com")