expressjs / serve-static

Serve static files
MIT License
1.38k stars 227 forks source link

Question: Is it possible to access the origin from setHeaders()? #145

Closed elcferreira closed 2 years ago

elcferreira commented 2 years ago

Hallo,

I'm having a problem to setup multiples domain to be Access-Control-Allow-Origin. I don't like to use * because of security issues, so I was trying to apply this solution bellow:

https://stackoverflow.com/questions/24897801/enable-access-control-allow-origin-for-multiple-domains-in-node-js

Is it possible to access the origin on the parameters returned on setHeaders?

setHeaders(res, path, stat) {}

Thank you for all help. ❤️

elcferreira commented 2 years ago

Solution:

I'm using nuxt, and I tried to do it on

render > setHeaders,

But i switch the logic to serverMiddleware and it works.

😄

dougwilson commented 2 years ago

Hi @elcferreira sorry you never got a response to your question. I actually swear I remember replying to this question (I guess the same question but somewhere else lol). Anyway, you can access the request object from the response object using res.req. The origin request header would thus be res.req.headers.origin. I know you already have a solution, but just wanted to answer your original one for your reference and anyone else who crosses this issue.

elcferreira commented 2 years ago

Hey @dougwilson I tried it inside the nuxt.js (v2.15.8) context, but I got undefined.

image
dougwilson commented 2 years ago

Hi @elcferreira hm.. I'm not sure exactly what nuxt.js is, as I haven't used it before. This particular module is an Express.js middleware. In Express.js, there is the res.req: https://expressjs.com/en/4x/api.html#res.req