Add an option to toggle handling of OPTIONS requests#484
Merged
darrachequesne merged 5 commits intosocketio:1.8.xfrom Mar 11, 2017
Merged
Add an option to toggle handling of OPTIONS requests#484darrachequesne merged 5 commits intosocketio:1.8.xfrom
darrachequesne merged 5 commits intosocketio:1.8.xfrom
Conversation
Member
|
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
}
}); |
Author
|
That would work too. I will implement it tomorrow. |
Author
|
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); |
Author
|
The last error on travis doesn't seem to be related to that PR. |
Member
|
@MiLk thanks a lot! |
darrachequesne
pushed a commit
to darrachequesne/engine.io
that referenced
this pull request
Mar 11, 2017
…#484) By setting `handlePreflightRequest` to false, OPTIONS request are no longer processed by engine.io. A function can also be provided.
darrachequesne
pushed a commit
that referenced
this pull request
May 8, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The kind of change this PR does introduce
Current behaviour
OPTIONS request are processed by engine.io
New behaviour
By setting
enableOptionstofalse, 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.