日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

npm模块之opn使用教程(node **.js直接再浏览器中打开相应的文件)

發布時間:2023/12/8 HTML 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 npm模块之opn使用教程(node **.js直接再浏览器中打开相应的文件) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安裝

$ npm install opn --save

?使用方法

const opn = require('opn');// opens the image in the default image viewer opn('unicorn.png').then(() => {// image viewer closed }); opn('unicorn.png').catch(() => {// image viewer closed });// opens the url in the default browser opn('http://sindresorhus.com');// specify the app to open in opn('http://sindresorhus.com', {app: 'firefox'});// specify app arguments opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});

opn(target, [options])

返回生成的子進程的promise。 你通常不需要使用這個任何東西,但它可以是有用的,如果你想附加自定義事件監聽器或直接對生成的進程執行其他操作。

target:
必需
類型:string

你想打開的東西。 可以是URL,文件或可執行文件/網址。

在默認應用中打開文件類型。 例如。 URL在您的默認瀏覽器中打開。

options:
類型:object

wait
類型: boolean
默認: true

等待打開的應用程序在調用callback之前退出。 如果為false,則會在打開應用程序時立即調用。

在Windows上,您必須顯式指定一個應用程序才能等待。

app:
類型: string,array

指定要使用的target打開的應用程序,或包含應用程序和應用程序參數的數組。

應用名稱取決于平臺。 不要在可重用模塊中硬編碼。 例如。 Chrome是OS X上的google chrome,Linux上的google-chrome和Windows上的chrome。

?

案例:

let http = require('http'); //引入opn模塊 let open = require('opn');let server = http.createServer((req, res) => {res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });res.write('hello!nodejs');res.end(); }); server.listen(3000);let defaultUrl = 'http://127.0.0.1:3000'; //open(默認打開的路徑,{打開的瀏覽器}).catch(沒有成功打開后的回掉函數) open(defaultUrl, { app: 'firefox' }).catch(() => {console.log('默認打開失敗!') })

?

?

?

總結

以上是生活随笔為你收集整理的npm模块之opn使用教程(node **.js直接再浏览器中打开相应的文件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。