这主要是为了在本地开发服务。
我使用Restify和CORS获得带有DELETE请求的405 method not allowed
。我想我一定是在忽略一些事情。非常感谢一些新鲜的眼睛指出我做错了什么。
我确实知道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
删除请求
请求
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
答案 0 :(得分:1)
好像您还没有为DELETE方法注册处理程序。答复显示了这一点:
允许:选项
也许您还没有正确连接处理程序?