79445517

Date: 2025-02-17 13:23:52
Score: 2
Natty:
Report link

If You are facing Problem to setup cross origin resourse sharing in Your App the Look How I am doing ;



export default function cors(req=request , res=response, next ) {
    try {
        res.setHeader('Cross-Origin-Resource-Policy'  , 'cross-origin' );
        res.setHeader('Access-Control-Allow-Origin'  ,'<CLIENT_ORIGIN>' || '*" );
        res.setHeader('Access-Control-Allow-Methods'  , '<Mehtods>' );
        res.setHeader('Access-Control-Allow-Headers'  , <HEADERS>' );
        res.setHeader('Access-Control-Allow-Credentials'  , 'true' );
        if (req.method.toLowerCase() === 'options') {
            return res.sendStatus(200)
        }
        next()
    } catch (error) {
        catchError(res, error)
    }
}

Look replace CLIENT_ORIGIN with you client origin url what will request the app api and add allowed methods like get,post,put and else and replace Mehtods .and same for the headers and if you want to send cookies than set 'Access-Control-Allow-Credentials' as True;

In request in OPTIONS Method Please send 200 status code

Reasons:
  • RegEx Blacklisted phrase (2.5): Please send
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Mubtasim Fuad