Reputation: 122
After i install CRA from npx create-react-app app-name
and then run yarn start
it will be start the development server in http://localhost:9000
by default.
The problem is, when i copy the url from browser address bar to my CORS middleware in the backend, the url showing http://localhost:9000/
. This is will throw error Access-Control-Allow-Origin
not allowed because slash in the end of url.
I want to change the url that showing on terminal after start CRA development server with slash in the end of url (http://localhost:9000/
), how its possible? IMHO, slash in the end of url make url prettier hehe. That's why i want to add it beside of CORS problem.
Thank you!
Upvotes: 2
Views: 1913
Reputation: 11697
There is no absolute rule for this. A trailing slash is optional. However general theory is to avoid trailing slash.
Here you can read more about this. Below is a snippet from the link provided.
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.
As per one of the post of Wikipedia, trailing slash indicates directory paths.
Adding a trailing "/" to a non-empty path. Directories (folders) are indicated with a trailing slash and should be included in URIs.
As you already understand, URL http:localhost:9000
is not same as http://localhost:9000/
. They are different URLs and that is the reason CORS does not work. CORS URL should be domains and not specific URLs. In your case, your domain is http://localhost:9000
and not http://localhost:9000/
. So I would truly avoid adding http://localhost:9000/
to CORS settings.
In my personal opinion, I would be sticking to the default behavior of browser and common notion in which URLs are perceived by most users until I have strong reason to change them. And for me, URL without additional slashes are more pretty :)
Upvotes: 3