1.

How To Allow Cors In Expressjs? Explain With An Example?

Answer»

In order to allow CORS in Express.js, add the following CODE in server.js:

app.all('*', FUNCTION(req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' == req.method) RETURN res.send(200);
next();
});

In order to allow CORS in Express.js, add the following code in server.js:

app.all('*', function(req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' == req.method) return res.send(200);
next();
});



Discussion

No Comment Found