问题
I’m trying to view a CreateReactApp project from a different device. I tried setting the host to 0.0.0.0
by setting the “start” script to:
"start": "cross-env HOST=0.0.0.0 react-scripts start".
Though the console did log
Attempting to bind to HOST environment variable: 0.0.0.0
If this was unintentional, check that you haven't mistakenly set it in your
shell.
Learn more here: [URL BLOCKED BY GITHUB].
I could not access this project from another device on the same network using the computer’s IP.
At the same time I managed to access other projects (from a “tomcat” local server) on the same computer from the same device using the computer's IP. Any idea why this might be happening? My environment: Windows10. I tried it on both ‘PowerShell’, and ‘Bash on Ubuntu on Windows (WSL)’, on more than one Create-React-App project.
回答1:
By default, Create React App serves the app in localhost:3000
. So accessing <ip address of machine serving the app>:3000
should work for devices in the same network.
Revert the change in start script in package.json and open the app in <ip adress>:3000
回答2:
Windows Defender Firewall was blocking the connection. It had a blocking rule for "NodeJs: Server Side JavaScript". To open the connection:
Settings => Update and Security => Windows Security => Firewall and network protection => Advanced Settings => Inbound rules => remove rules blocking node server.
Windows did not show a notification when blocking. It might be that at some point for some reason I authored it to block a connection to the nodeJS server.
来源:https://stackoverflow.com/questions/50703523/viewing-nodejs-projects-on-other-devices-in-the-network