简体   繁体   English

与运行新React App有关的问题

[英]Issue related to running of New React App

I installed Nodejs(v 10.16.0 LTS) and then using Windows Powershell I run following commands 我安装了Nodejs(v 10.16.0 LTS),然后使用Windows Powershell运行以下命令

     npx create-react-app my-app
     cd my-app
     npm start

The actual problem I am facing is that if PowerShell window is opened in the background, then my code works properly. 我面临的实际问题是,如果在后台打开PowerShell窗口,则我的代码可以正常工作。 But when I close PowerShell and reload Browser Tab, the error occurs which say 但是,当我关闭PowerShell并重新加载“浏览器”选项卡时,发生错误,提示

This site can't be reached 无法访问该网站

localhost refused to connect. 本地主机拒绝连接。

Try: 尝试:

Checking the connection 检查连接

Checking the proxy and the firewall. 检查代理和防火墙。

Kindly help me to figure out this issue. 请帮助我解决这个问题。 Thanks 谢谢

When you close PowerShell window you terminate your React app as well, it has to stay opened. 当关闭PowerShell窗口时,您也将终止React应用程序,它必须保持打开状态。 Running npm start kicks off local server with your app running. 运行npm start在您的应用运行npm start启动本地服务器。 If you want to have possibility to have application running with terminal closed you can use libs like https://www.npmjs.com/package/forever , but I do not recommend that- it's easy to forget you have one app instance running already :) 如果您希望关闭终端运行的应用程序可以使用https://www.npmjs.com/package/forever之类的库,但是我不建议这样做-很容易忘记您已经运行了一个应用程序实例:)

When you run the command npm start , this is the expected behaviour. 当您运行命令npm start ,这是预期的行为。 This command spawns a new npm instance in the directory from where you have executed it and directs it to start a server. 此命令在执行它的目录中生成一个新的npm实例,并指示它启动服务器。 The catch here is that the server runs as an attached process with PowerShell, by default and it is bound to terminate once you close the PowerShell window. 这里的问题是服务器默认情况下作为PowerShell的附加进程运行,并且一旦关闭PowerShell窗口,它必然会终止。

To manually detach a process from the terminal, we use an & after the command on a UNIX-based system (Linux, MacOS etc.) something like npm start & .In case of PowerShell, you can make use of a built-in function called Start-Process like this - 要从终端手动分离进程,我们在基于UNIX的系统(Linux,MacOS等)上的命令后使用& ,例如npm start & 。在PowerShell中,您可以使用内置函数像这样称为“ 启动过程”

Start-Process -NoNewWindow npm start

If you'd like to read more about it, you can refer a blog post here - https://ariefbayu.xyz/run-background-command-in-powershell-8ea86436684e 如果您想了解更多信息,可以在这里引用博客文章-https: //ariefbayu.xyz/run-background-command-in-powershell-8ea86436684e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM