CORS Archives - wiki

Allow CORS in Express/Node.js?

Open index.js (in your case it may be app.js or server.js) and set the request header

var express = require('express');
var app = express();
...
...

//CORS support settings
app.use(function(req, res, next){
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    next();
});


...

....

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

app.use('/api/user', user);

...

 

By bm on August 17, 2016 | Express, NodeJs | A comment?
Tags: , ,