405方法不允许使用带有Restify和CORS的DELETE

问题描述:

这主要是为了在本地开发服务。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 

看来好像你尚未为您的DELETE方法注册处理程序。响应显示了这个:

允许:OPTIONS

也许你没有正确连接了处理器?