-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to toggle handling of OPTIONS requests #484
Conversation
Thanks for the pull request! What do you think of a var io = require('socket.io')(server, {
handlePreflightRequest: false,
// or
handlePreflightRequest: (res, req) => {
// set the proper headers
}
}); |
That would work too. I will implement it tomorrow. |
You can now do: var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server, {
handlePreflightRequest: (req, res) => {
let headers = {};
if (req.headers.origin) {
headers['Access-Control-Allow-Credentials'] = 'true';
headers['Access-Control-Allow-Origin'] = req.headers.origin;
} else {
headers['Access-Control-Allow-Origin'] = '*';
}
headers['Access-Control-Allow-Methods'] = 'GET,HEAD,PUT,PATCH,POST,DELETE';
headers['Access-Control-Allow-Headers'] = 'origin, content-type, accept';
res.writeHead(200, headers);
res.end();
}
});
io.on('connection', function(){ /* … */ });
server.listen(3000); Or: var app = require('express')();
var cors = require('cors');
app.options('*', cors());
var server = require('http').Server(app);
var io = require('socket.io')(server, {
enableOptions: false
});
io.on('connection', function(){ /* … */ });
server.listen(3000); |
The last error on travis doesn't seem to be related to that PR. |
@MiLk thanks a lot! |
…#484) By setting `handlePreflightRequest` to false, OPTIONS request are no longer processed by engine.io. A function can also be provided.
The kind of change this PR does introduce
Current behaviour
OPTIONS request are processed by engine.io
New behaviour
By setting
enableOptions
tofalse
, OPTIONS request are no longer processed by engine.ioOther information (e.g. related issues)
Related to #279
This allows to handle CORS ourselves:
I've raised the PR against the 1.8.x branch, as it's the version used by socket.io at the moment.