[英]Create React App: how to get final port dev-server running on
From https://facebook.github.io/create-react-app/docs/advanced-configuration you can provide your own script to BROWSER
environment variable.从https://facebook.github.io/create-react-app/docs/advanced-configuration,您可以向
BROWSER
环境变量提供您自己的脚本。
On this script, I want to open link to some other page, with a query parameter to redirect to port the dev-server is running on localhost.在此脚本中,我想打开指向其他页面的链接,并使用查询参数重定向到开发服务器在本地主机上运行的端口。
Even if I do BROWSER=open.js PORT=4000 npm run start
, port 4000
may be taken, which is what choosePort
from https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/start.js#L87 does;即使我做
BROWSER=open.js PORT=4000 npm run start
,端口4000
可以采取,这是choosePort
从https://github.com/facebook/create-react-app/blob/master/packages/react -scripts/scripts/start.js#L87可以; it finds the first port available around the provided port.它在提供的端口周围找到第一个可用的端口。
My question is this: given my open.js
, how can I find the actual PORT the dev-server is running on?我的问题是:鉴于我的
open.js
,我怎样才能找到运行开发服务器的实际端口?
I found that that the process.argv
does in fact contain the URL (with port) of your dev-server.我发现
process.argv
实际上包含你的开发服务器的 URL(带端口)。
console.log(process.argv)
Provides:提供:
[ '/usr/local/bin/node',
'/path/to/my/custom/openBrowser.js',
'http://localhost:3000/' ]
To get current port you can use:要获取当前端口,您可以使用:
console.log(window.location.port)
window.location
also provides more useful information. window.location
还提供了更多有用的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.