简体   繁体   English

创建 React App:如何让最终端口 dev-server 运行

[英]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可以采取,这是choosePorthttps://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.

相关问题 Webpack开发服务器和React Server端渲染 - Webpack Dev-Server And React Server Side rendering 如何相对于root更改webpack开发服务器输出publicPath - How to change webpack dev-server output publicPath relative to root Webpack开发服务器-如何为具有更新资产的静态文件提供服务? - Webpack dev-server - how to serve a static file with updated assets? 在 Windows 10 上运行 npm run dev-server 时出错 - Getting Error while running npm run dev-server on windows 10 Cufon在开发服务器和已发布版本上的差异 - Cufon differences on dev-server and published version Webpack 开发服务器并做出反应。 仅在运行应用程序时获取目录列表 - Webpack dev server and react. Only get directory listing when running app 未定义Perf - 使用webpack-dev-server运行react app - Perf is not defined - running react app with webpack-dev-server 在Docker容器中使用带有Webpack dev-server的服务工作者 - Use service workers with Webpack dev-server inside Docker container Webpack:在没有开发服务器的情况下,设置用于访问API的CORS标头 - Webpack: without dev-server, setting CORS headers for accessing API 使用 create-react-app 和 webpack dev server 将来自客户端反应应用程序的请求代理到服务器 - Proxying requests from client react app to server with create-react-app and webpack dev server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM