Qin*_* Xu 118 javascript node.js
我正在使用Node.js编写应用程序.
我想要创建的功能之一是打开默认Web浏览器并导航到特定URL.
我希望它是便携式的,以便它可以在Windows/Mac/Linux上运行.
For*_*say 150
使用opn因为它将处理跨平台问题.安装:
$ npm install opn
Run Code Online (Sandbox Code Playgroud)
使用:
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'});
Run Code Online (Sandbox Code Playgroud)
lex*_*a-b 28
var url = 'http://localhost';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);
Run Code Online (Sandbox Code Playgroud)
Oli*_*r C 10
const open = require('open')
await open('http://sindresorhus.com') // Opens the url in the default browser
await open('http://sindresorhus.com', {app: 'firefox'}) // Specify the app to open in
Run Code Online (Sandbox Code Playgroud)
小智 7
安装:
$ npm install open
Run Code Online (Sandbox Code Playgroud)
用法:
const open = require('open');
(async () => {
// Opens the image in the default image viewer and waits for the opened app to quit.
await open('unicorn.png', {wait: true});
console.log('The image viewer app quit');
// Opens the URL in the default browser.
await open('https://sindresorhus.com');
// Opens the URL in a specified browser.
await open('https://sindresorhus.com', {app: 'firefox'});
// Specify app arguments.
await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
})();
Run Code Online (Sandbox Code Playgroud)
Windows + Express
app.listen(3000, ()=>{
require('child_process').exec('start http://localhost:3000/');
});
Run Code Online (Sandbox Code Playgroud)
您可能需要使用 ...
require('os').type()
Run Code Online (Sandbox Code Playgroud)
然后使用spawn("open")
或spawn("xdg-open")
取决于平台?
归档时间: |
|
查看次数: |
68389 次 |
最近记录: |