0

I'm developing a web app using node, express, Vue3

I'm trying to post a form to an endpoint on my router at '/match/matchpost' I've tested this end point and it't worked in the past. I recently finished setting up SSL and after that i began to get the following errors

r.js:247     GET https://localhost:5173/ net::ERR_FAILED
dispatchXhrRequest @ xhr.js:247
xhr @ xhr.js:49
dispatchRequest @ dispatchRequest.js:51
request @ Axios.js:142
httpMethod @ Axios.js:181
wrap @ bind.js:5
submitForm @ MatchForm.vue:18
(anonymous) @ MatchForm.vue:78
(anonymous) @ runtime-dom.esm-bundler.js:1483
callWithErrorHandling @ runtime-core.esm-bundler.js:173
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:182
invoker @ runtime-dom.esm-bundler.js:345
matchform:1 Access to XMLHttpRequest at 'https://localhost:5173/' (redirected from 'https://localhost:8080/match/matchpost') from origin 'https://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
MatchForm.vue:35 AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …}

In the browser console it's showing as if im trying to make a get request, despite the fact that im not, as you can see here.

            axios.post("https://localhost:8080/match/matchpost",{
                title: this.title,
                game: this.game,
                description: this.description,
                rules: this.rules,
                participantNum: this.participantNum,
            },{
                headers:{
                    'Content-Type' : 'application/json'
                },
                withCredentials: true
            })
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.log(error);
            });

as you can see im using the whole url in the post request, that's a separate smaller problem for a different day but that has worked up until now.

in addition i can see in my VScode console that the request is showing as a post

[Server] Received POST request at /matchpost
[Server] [2023-05-16T02:43:44.097Z] Request to Match Router: POST /match/matchpost

clearly those are not the only issues but i suspect that they stem from the CORS error that is telling me that I can't use wildcards with credentials. The problem is I'm not using a wildcard as best I can tell.

(the browser request header does show a wildcard by the way)

I have tried searching extensively for people who have come across this problem, but I have failed to find anyone who has had the same problem.

I have tried all manner of different CORS configurations with no success.

I had CORS configured on a per router basis and as i thought perhaps one set up was interfering with another i put my CORS configuration all into one file and have imported it where needed.

here it is below

const cors = require('cors');

const corsOptions = {
  origin: 'https://localhost:5173',  
  credentials: true,
  methods: ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE', 'HEAD'],
  allowedHeaders: ['auth-token', 'Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
  preflightContinue: true,
};


const corsMiddleware = cors(corsOptions);

module.exports = corsMiddleware;

and here is how I'm importing it.

match.use(corsMiddleware);
match.options('*', corsMiddleware);

I'll be happy to provide any more information upon request. I've tried to include anything you may need but as I'm at a loss to what the actual problem is I may be leaving something out.

Thanks for any help!

  • you say it requires credentials but you don't pass any credentials. Also, localhost is notoriously bad with CORS. It basically isn't supported. – Garr Godfrey May 16 '23 at 04:17

1 Answers1

0

Testing CORS on localhost is problematic. There are scenarios that just won't work.

In your case, however, the problem is you are doing a redirect from your POST handler.

When a POST responds with a redirect, it is resent as a GET request (depending on the exact response code. A 303 will always redirect as GET. 301 is supposed to ask the user.)

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
  • see https://stackoverflow.com/questions/10883211/why-does-my-http-localhost-cors-origin-not-work – Garr Godfrey May 16 '23 at 04:29
  • and https://stackoverflow.com/questions/33214717/why-post-redirects-to-get-and-put-redirects-to-put – Garr Godfrey May 16 '23 at 04:30
  • You write: _Testing CORS on localhost is problematic. There are scenarios that just won't work._ I don't know what you have in mind... Things will work fine if the adequate `localhost` Web origin is allowed in the CORS configuration. – jub0bs May 16 '23 at 07:15
  • Thanks for the reply. I have read the suggested articles. just a couple question. when you say CORS and localhost is problematic do you mean actually developing locally or using the domain local host? like would just pointing at 127.0.0.1 work better? Also where are you seeing a redirect? I'm guess it's something going on behind the scenes as I don't see an obvious redirect. – Jerome Zerovnik May 16 '23 at 22:23
  • 127.0.0.1 has the same thing. Chrome had bugs supporting it, at least with wildcard, and maybe you don't have that. One of those links describes using a `hosts` file to map something like `localho.st` to 127.0.0.1 – Garr Godfrey May 18 '23 at 05:44