我想知道当我用 nodejs 启动服务器时是否以及如何自动打开网页
app.listen(app.get("PORT"), () => {
console.log(`Servidor corriendo en http://localhost:${app.get("PORT")}`)
})
我有一个基本示例,当我运行 node start proyect 时,它将启动服务器,然后我需要手动单击控制台记录的消息或打开浏览器并键入 bla bla bla
我想像在 React 中运行命令 npm run start
一样自行提升该网络我试着用谷歌搜索它,但我不知道如何搜索它,因为我没有得到任何有用的信息。 非常感谢你
最佳答案
使用open打开 URL、文件、可执行文件等内容。跨平台。
const open = require('open');
// opens the url in the default browser
open('http://stackoverflow.com');
更新
open 简化了跨平台功能,在 node 中你可以通过 process.platform
访问平台并编写平台特定的执行
例如这里是没有使用任何NPM模块的mac原生版本
const url = 'http://stackoverflow.com';
require('child_process').exec(`open ${url}`);
我没有访问 windows 系统的权限来为 windows 编写代码
关于node.js - 启动nodejs服务器时打开网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67218937/