0

Iam unable to configure properly a OPTIONS request. The POST request works perfectly on local development. Also with IE. But live site with Chrome or Firefox get stuck after OPTIONS request. What I mised here?

Request Sample of live site. Request OPTIONS could response 200 or 204 (No content) with different express config. Post request is not sent with actual config!

"OPTIONS /api/books HTTP/1.1" 200 -

express: I tried several configurations on Express server. Added all to try. Like:

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Token');
    res.header("Access-Control-Allow-Credentials", true);
    next();
});

nginx config:

server {
listen 80;
server_name xxxxxxxx.com;

location / {
    proxy_pass_request_headers on;
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

}
}

Angular: The frontend uses a library to upload an image File upload. I Tried to add specific fields:

Upload.upload({
                url: uploadUrl,
                method:'POST',
                withCredentials: true,
                headers: {
                  'Content-Type': 'multipart/form-data'
                },
                data: data
            }

curl -X OPTIONS http://www.xxxxxxxxxx.com/api/books -i

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 12 Oct 2017 07:10:39 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3792
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type,     Accept, Authorization, Token
Access-Control-Allow-Credentials: true
....

I miss OPTIONS method on last list. Is this a Express mistake or nginx?

Edit 1: New config.

j.son
  • 21
  • 1
  • 5
  • nginx server you're hitting is not setup to accept options requests: Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE – Jay Lane Sep 21 '17 at 19:43
  • Try adding `proxy_pass_request_headers on;` to your `proxy_pass` block. This will allow headers to propogate from your NODE app – Tarun Lalwani Sep 22 '17 at 06:25
  • Added config from the blog referenced here: [link](https://stackoverflow.com/a/9197143/5166058). But now I cant see either OPTIONS or POST request on expres trace. Also tried with `proxy_pass_request_headers on;` – j.son Sep 22 '17 at 12:19

0 Answers0