cors request #316
|
I could not find any information at all on this topic, is it even possible to setup cors in uWS.js? since I need server permission to accept a request from a specific Url but there is simply no information about this. I'm trying to just get data from a GET server with a request with a cors header, |
Replies: 8 comments 12 replies
|
Why do think it would not be possible? There is a ton of information on the internet about this, have you tried to google cors? Seems like you do not know what cors is to begin with. |
|
On stackoverflow could get rid of such comments (go to google), but unfortunately they still remained here. Naturally, I already read this information on mdn, and yes, I'm not a backend developer, maybe I don’t understand something, I look at the analogue of uWS (express) and I see the use of middleware there for such purposes, and try to understand how I can do something like this in uWS.
|
|
@MoJongleur uws does not have middlewares, so you need to write that yourself. How cors works I suggest you take a look how express is doing it https://github.com/expressjs/cors/blob/master/lib/index.js is a good start I suggest you learn more about how express works internally (which is not that complicated) and basic backend stuff |
|
Isn't this really just setting a header to your response? |
Initially, I thought so, but my poor understanding of the topic did not allow me to understand that this should be done not only in GET but also in OPTION. Anyway, thanks to everyone for the advice, and sorry for such stupid questions) |
|
Once we have github discussions here we can have a more stackoverflow-ish community where anything goes. |
|
@mtsewrs Setting up cors is not just about adding the access-control-allow-origin header. It also requires serving the preflight requests for non-simple requests. So the OP's issue deserves a better answer than yours. @MoJongleur In your case of sending a GET request for retrieving data, you'll just need to serve the header function setCorsHeaders(response) {
// You can change the below headers as they're just examples
response.writeHeader("Access-Control-Allow-Origin", "*");
response.writeHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response.writeHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with");
response.writeHeader("Access-Control-Max-Age", "3600");
}
app.options("/graphql", (response) => {
setCorsHeaders(response);
response.end();
});
app.post("/graphql", (response) => {
setCorsHeaders(response);
// handler requests...
});For more details about preflight requests, you can take a look at this article. Basically, uwebsocket.js is a low level library. I actually do not recommend using this lib if you want something to work smoothly right out of the box. This answer is a bit late but I think it might still help somebody else struggling with cors stuff. |
|
With an ecosystem, we can choose to not reinvent the wheel. I created a CORS plugin µWS CORS (under µWS Ecosystem). Feel free to contribute, to review, to recommend new projects/middlewares/plugins or whatever. |
Initially, I thought so, but my poor understanding of the topic did not allow me to understand that this should be done not only in GET but also in OPTION.
Anyway, thanks to everyone for the advice, and sorry for such stupid questions)