如何启动Node服务器并在浏览器中打开本地主机?

huangapple go评论46阅读模式
英文:

How can I launch node server and open localhost in the browser after there?

问题

以下是翻译好的部分:

我有一个简单的Node.js项目和在Bash中的脚本。在启动服务器之前,我使用了一些命令,之后我需要在本地打开浏览器。
Bash脚本:

nodemon server.js
xdg-open https://localhost:3007

我该如何在启动Node服务器后打开本地主机中的浏览器?

英文:

I have the simple node.js project and script on the bash. I use a few commands before launching the server and after there I need to open browser on the localhost.
bash script:

nodemon server.js
xdg-open https://localhost:3007

How can I launch node server and open localhost in the browser after there?

答案1

得分: 0

安装 open 模块:

npm install open

在 server.js 中添加:

const open = require('open');
open('http://localhost:3007');

启动项目:

npm start

Node 服务器已启动,并在 http://localhost:3007 上打开浏览器。

英文:

Install open module:

npm install open

Add to the server.js:

const open = require('open');
open('http://localhost:3007');

Launch project:

npm start

Node server was started and open browser on the http://localhost:3007

答案2

得分: 0

你可以在后台打开 nodemon,然后打开浏览器:

nodemon server.js &
xdg-open https://localhost:3007

或者作为一行,没有任何区别:

nodemon server.js & xdg-open https://localhost:3007
英文:

You can open nodemon in the background and then open the browser:

nodemon server.js &
xdg-open https://localhost:3007

Or as a single line, it doesn't make any difference:

nodemon server.js & xdg-open https://localhost:3007

huangapple
  • 本文由 发表于 2023年2月8日 23:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定