0

I have enabled CORS in my nodejs server that is using express as a middleware but i am still getting

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

I have tried enabling cors and solutions suggested here Using CORS Still Give Cross Origin Error

But it still doesn't work for me.

app.use(cors())
app.options('*',cors())
app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');

    // authorized headers for preflight requests
    // https://developer.mozilla.org/en-US/docs/Glossary/preflight_request
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();

    app.options('*', (req, res) => {
        // allowed XHR methods  
        res.header('Access-Control-Allow-Methods', 'GET, PATCH, PUT, POST, DELETE, OPTIONS');
        res.send();
    });
});

This how i am creating request in front-end nextjs app.

 const requestBody={
            bnb: address
        }


        let config = {
            headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
            }
        }


        axios.post(methods.verify_bnb, qs.stringify(requestBody), config)
        //

I should be able to make request and server to process it as i have enabled cors but it is still throwing mentioned errors.

Saeedi
  • 23
  • 1
  • 5
  • 1
    To make sure cors is configured here, please take a look here: https://expressjs.com/en/resources/middleware/cors.html . Are you getting 503 error code? I saw once how it was interpretaded as cors error in chrome browser - in my example i got this when server had problems with database call when processing request. – Alex Sep 24 '19 at 11:10
  • I did followed instructions from link provided for this app but still no use. No i am not getting 503 error either. It is not producing error on local environment. – Saeedi Sep 24 '19 at 11:20
  • did you try using cors like this ? const cors = require('cors') app.use(cors()) , I think right now you are setting cors and then overwriting those settings. Or look here if want to allow it for specific routes https://stackoverflow.com/questions/11181546/how-to-enable-cross-origin-resource-sharing-cors-in-the-express-js-framework-o – Alex Sep 24 '19 at 13:06
  • Yes i am using this config. I have removed the other ones but error still persists. It is worth mentioning that i have deployed server on google compute engine. I am able to interact it with postman with http but on app it gives error of blocked mixed content so i have converted it to https, it doesn't work with postman but app doesn't create that error but cors error – Saeedi Sep 24 '19 at 15:48
  • Did you check google compute engine logs? – Alex Sep 24 '19 at 18:17
  • Yes i did. But i solved the issue by deploying front end and server both on the same compute engine. – Saeedi Sep 26 '19 at 08:00

1 Answers1

0

I deployed my front-end on zeit and server on compute engine. This was giving me this error, as soon as i deployed front-end on the same compute engine, it worked fine. I am not sure if thats the right implementation but CORS error is not producing anymore

Saeedi
  • 23
  • 1
  • 5