启动nodejs服务器时打开网页

Pro*_*r89 3 node.js express

我想知道当我用nodejs启动服务器时是否以及如何自动打开网页

app.listen(app.get("PORT"), () => {
        console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
    })
Run Code Online (Sandbox Code Playgroud)

我有这个基本示例,当我运行node start proyect时,它将启动服务器,然后我需要手动单击控制台记录的消息或打开浏览器并输入 bla bla bla

我想自行提升该网络,就像在 React 中运行命令npm run start一样

我尝试用谷歌搜索它,但我不知道如何搜索它,因为我没有得到任何有用的信息。十分感谢

Rob*_*bot 7

使用open来打开 URL、文件、可执行文件等内容。跨平台。

const open = require('open');

// opens the url in the default browser 
open('http://stackoverflow.com');
Run Code Online (Sandbox Code Playgroud)

更新

process.platformopen 简化了跨平台功能,在节点中您可以通过编写特定于平台的执行来访问平台

例如,这是 mac 中的本机版本,不使用任何 NPM 模块

const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);
Run Code Online (Sandbox Code Playgroud)

我无法访问Windows系统来编写Windows代码