日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > HTML >内容正文

HTML

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

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

安裝

$ 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])

返回生成的子進(jìn)程的promise。 你通常不需要使用這個(gè)任何東西,但它可以是有用的,如果你想附加自定義事件監(jiān)聽(tīng)器或直接對(duì)生成的進(jìn)程執(zhí)行其他操作。

target:
必需
類型:string

你想打開(kāi)的東西。 可以是URL,文件或可執(zhí)行文件/網(wǎng)址。

在默認(rèn)應(yīng)用中打開(kāi)文件類型。 例如。 URL在您的默認(rèn)瀏覽器中打開(kāi)。

options:
類型:object

wait
類型: boolean
默認(rèn): true

等待打開(kāi)的應(yīng)用程序在調(diào)用callback之前退出。 如果為false,則會(huì)在打開(kāi)應(yīng)用程序時(shí)立即調(diào)用。

在Windows上,您必須顯式指定一個(gè)應(yīng)用程序才能等待。

app:
類型: string,array

指定要使用的target打開(kāi)的應(yīng)用程序,或包含應(yīng)用程序和應(yīng)用程序參數(shù)的數(shù)組。

應(yīng)用名稱取決于平臺(tái)。 不要在可重用模塊中硬編碼。 例如。 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(默認(rèn)打開(kāi)的路徑,{打開(kāi)的瀏覽器}).catch(沒(méi)有成功打開(kāi)后的回掉函數(shù)) open(defaultUrl, { app: 'firefox' }).catch(() => {console.log('默認(rèn)打開(kāi)失敗!') })

?

?

?

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。