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, |
Beta Was this translation helpful? Give feedback.
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. |
Beta Was this translation helpful? Give feedback.
-
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.
|
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Isn't this really just setting a header to your response? |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
Once we have github discussions here we can have a more stackoverflow-ish community where anything goes. |
Beta Was this translation helpful? Give feedback.
-
@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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
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)