我已經閱讀了許多文章,並嘗試了幾乎所有我能想到的配置使CORS與restify一起工作。我已將restify.CORS()
與restify.fullResponse()
和其他所有組合一起使用。我也嘗試使用Cors lib(npm install cors)無濟於事。我的應用程序是這樣的:無法使CORS與restify一起工作,所有的預檢選項返回405
/**
* Setup middlewares.
*/
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.cors());
server.use(restify.fullResponse());
server.use(morgan('dev'));
我也嘗試添加OPTS與處理:
server.opts('/\.*/', (req, res, next) => {
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS, DELETE');
res.send(204);
return next();
});
在任何情況下,我得到的結果:
XMLHttpRequest cannot load http://localhost:3000/1.0.0/clients. Response to preflight
request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'http://run.plnkr.co' is therefore not
allowed access. The response had HTTP status code 405.
任何想法?這是與restify 4.0.3。謝謝!
類型錯誤:restify.CORS是不是一個功能 –
它已經過時:https://github.com/TabDigital/restify-cors-middleware –