My frontend is developed via Nuxt.js (Vue.js) and hosted on Github Pages. I have successfully confirmed that my Glitch-hosted backend is able to serve my locally compiled (development mode). It is also confirmable since the above link shows a lengthy JSON data.
However, my ‘Github Pages’ hosted static website is unable to get API data. The ERROR message simply says I have a ‘405’ error code - this is the ‘Method Not Allowed’.
I initially thought I had a CORS problem since I was giving a PORT number of 3001. However, even after enabling CORS for every website, this problem still persists, so this is not CORS.
My current suspicion is that Glitch is not allowing Github Page hosted website, since the error code 405 is mysterious and not easy to resolve.
Nah, I mean I was sending the options object with the port field of 3001 previously. I decided not to send any port field since I don’t know the port number used by glitch. Not sending any port number (or anything) means CORS is supported for all external websites.
Basically app.use(cors({ port: 3001 })); vs. app.use(cors());
Your reply on the other post was seems helpful. I’ll try to set a header for ‘User-Agent’ field with that string and come back.
@khalby786
Thanks for your help. I have now solved my problem.
I am not sure if it’s from the ‘User-Agent’ field, but I did that and also more importantly, I hardcoded my server API prefix in my GET and POST methods: https://tenzinhub-api.glitch.me/api
(Previously, as is the norm, my nuxt.config.js file has proxy handler etc that automatically converts GET method URI to have the server URL prefix.)
I think Github Pages being static-only hosting site is a major factor since imo the proxy handler creates the api endpoint at client side and redirects to the server side - Github Pages may not support this imo.
(Edit: I missed prefixing server URL for one POST method and that gave me the 405 error again. I fixed it and that proves giving full server URL as prefix is really needed. The ‘User-Agent’ is also probably important, as I am using that here. no harm done)
My first update with the ‘User-Agent’ field didn’t immediately work, then I hardcoded my server URL prefix with the '/api', it could be that there was a delay and the fix could have been the 'User-Agent'.
Anyways, one of these 2 fixed my problem. Now I can make more full-stack projects in the future. Thanks.