2017-10-10 56 views
0

這主要是爲了在本地開發服務。405方法不允許使用帶有Restify和CORS的DELETE

我收到一個405 method not allowed DELETE請求使用Restify和CORS。我想我必須忽略一些東西。真的會感激一些新鮮的眼睛指出我做錯了什麼。

我知道restify-cors-middleware,但我沒有使用它,因爲它沒有很好的記錄,我也沒有能夠正確配置它。

相反,我已經爲Restify實現了我自己的CORS配置。它適用於GET和POST,但不適用於DELETE。

// allows localhost to work 
if (process.env.DEV === 'true') { 
    app.pre((req, res, next) => { 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With'); 
    res.header('Access-Control-Allow-Credentials', 'true'); 
    res.header('Access-Control-Allow-Methods', 'DELETE, GET, POST, OPTIONS, PUT'); 
    res.header('access-control-max-age', 86400); 

    return next(); 
    }); 

    app.opts('/.*/', (req, res, next) => { 
    res.send(200); 
    return next(); 
    }); 
} 

選項預檢:

請求

URL: http://localhost:8000/f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe 
Request Method:OPTIONS 
Status Code:200 OK 
Remote Address:[::1]:8000 
Referrer Policy:no-referrer-when-downgrade 

響應頭

HTTP/1.1 200 OK 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With 
Access-Control-Allow-Credentials: true 
Access-Control-Allow-Methods: DELETE, GET, POST, OPTIONS, PUT 
access-control-max-age: 86400 
Date: Tue, 10 Oct 2017 01:04:57 GMT 
Connection: keep-alive 
Transfer-Encoding: chunked 

請求頭

OPTIONS /f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe HTTP/1.1 
Host: localhost:8000 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 
Access-Control-Request-Method: DELETE 
Origin: http://localhost:3000 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 
Access-Control-Request-Headers: authorization 
Accept: */* 
Referer: http://localhost:3000/ 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.8 

DELETE請求:

請求

URL: http://localhost:8000/f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe 
Request Method:DELETE 
Status Code:405 Method Not Allowed 
Remote Address:[::1]:8000 
Referrer Policy:no-referrer-when-downgrade 

響應頭

HTTP/1.1 405 Method Not Allowed 
Server: Planet Timelapse 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With 
Access-Control-Allow-Credentials: true 
Access-Control-Allow-Methods: DELETE, GET, POST, OPTIONS, PUT 
access-control-max-age: 86400 
Allow: OPTIONS 
Content-Type: application/json 
Content-Length: 61 
Date: Tue, 10 Oct 2017 01:04:57 GMT 
Connection: keep-alive 

請求頭

DELETE /f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe HTTP/1.1 
Host: localhost:8000 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 
accept: application/json 
Origin: http://localhost:3000 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 
Referer: http://localhost:3000/ 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.8 

回答

1

看來好像你尚未爲您的DELETE方法註冊處理程序。響應顯示了這個:

允許:OPTIONS

也許你沒有正確連接了處理器?