在 CRA 中是否有在 URL 末尾添加斜杠的设置?
Is there a setup to add slash in the end of URL in CRA?
在我从 npx create-react-app app-name
安装 CRA 之后 运行 yarn start
它会默认在 http://localhost:9000
启动开发服务器。
问题是,当我将 url 从浏览器地址栏复制到后端的 CORS 中间件时,url 显示 http://localhost:9000/
。这将抛出错误 Access-Control-Allow-Origin
not allowed because slash in the end of url.
我想更改启动 CRA 开发服务器后在终端上显示的 url 并在 url (http://localhost:9000/
) 末尾添加斜杠,这怎么可能?恕我直言,url 末尾的斜线使 url 更漂亮呵呵。这就是为什么我想在 CORS 问题旁边添加它。
谢谢!
这没有绝对的规则。尾部斜杠是可选的。然而,一般理论是避免尾部斜杠。
Here 您可以阅读更多相关信息。以下是所提供的 link 的片段。
You need to look at the HTTP request the browser makes to the server.
The URL displayed in the "status bar of the browser", or even the URL
in the address bar could be different - more user friendly. Google
Chrome does not append the trailing slash in the status bar, although
the slash is present in the request.
根据 Wikipedia 的 post 之一,尾部斜杠表示目录路径。
Adding a trailing "/" to a non-empty path. Directories (folders) are indicated with a trailing slash and should be included in URIs.
如您所知,URL http:localhost:9000
与 http://localhost:9000/
不同。它们是不同的 URL,这就是 CORS 不起作用的原因。 CORS URL 应该是域而不是特定的 URL。在您的例子中,您的域是 http://localhost:9000
而不是 http://localhost:9000/
。所以我真的会避免将 http://localhost:9000/
添加到 CORS 设置中。
在我个人看来,我会坚持浏览器的默认行为和大多数用户认为 URLs 的普遍观念,直到我有充分的理由改变它们。对我来说,没有额外斜杠的 URL 更漂亮 :)
在我从 npx create-react-app app-name
安装 CRA 之后 运行 yarn start
它会默认在 http://localhost:9000
启动开发服务器。
问题是,当我将 url 从浏览器地址栏复制到后端的 CORS 中间件时,url 显示 http://localhost:9000/
。这将抛出错误 Access-Control-Allow-Origin
not allowed because slash in the end of url.
我想更改启动 CRA 开发服务器后在终端上显示的 url 并在 url (http://localhost:9000/
) 末尾添加斜杠,这怎么可能?恕我直言,url 末尾的斜线使 url 更漂亮呵呵。这就是为什么我想在 CORS 问题旁边添加它。
谢谢!
这没有绝对的规则。尾部斜杠是可选的。然而,一般理论是避免尾部斜杠。
Here 您可以阅读更多相关信息。以下是所提供的 link 的片段。
You need to look at the HTTP request the browser makes to the server. The URL displayed in the "status bar of the browser", or even the URL in the address bar could be different - more user friendly. Google Chrome does not append the trailing slash in the status bar, although the slash is present in the request.
根据 Wikipedia 的 post 之一,尾部斜杠表示目录路径。
Adding a trailing "/" to a non-empty path. Directories (folders) are indicated with a trailing slash and should be included in URIs.
如您所知,URL http:localhost:9000
与 http://localhost:9000/
不同。它们是不同的 URL,这就是 CORS 不起作用的原因。 CORS URL 应该是域而不是特定的 URL。在您的例子中,您的域是 http://localhost:9000
而不是 http://localhost:9000/
。所以我真的会避免将 http://localhost:9000/
添加到 CORS 设置中。
在我个人看来,我会坚持浏览器的默认行为和大多数用户认为 URLs 的普遍观念,直到我有充分的理由改变它们。对我来说,没有额外斜杠的 URL 更漂亮 :)