在 Keystone.js API 中处理 OPTIONS 请求
Handling an OPTIONS request in a Keystone.js API
我在 Keystone 的支持下与 Aurelia 一起构建 SPA。
当从我的 Aurelia 视图中 POST 时,Content-Type
被设置为 application/json
,这当然会在来自客户端时引发 OPTIONS
请求-边.
我的 Keystone init 设置在 cors 上完全开放用于本地开发:
keystone.init({
...
'cors allow origin': true,
'cors allow methods': true,
'cors allow headers': true,
...
});
我的 API Keystone 路由绑定是:
exports = module.exports = function(app) {
app.all('/api/*', keystone.middleware.cors);
app.post('/api/inquiry', keystone.middleware.api, routes.api.inquiries.post);
};
我的 Keystone API 视图是:
var keystone = require('keystone'),
Inquiry = keystone.list('Inquiry');
exports.post = function(req, res) {
var inquiry = new Inquiry.model({
name: {
first: req.body.name.first,
last: req.body.name.last
},
email: req.body.email,
phone: req.body.phone,
question: req.body.question
});
inquiry.save(function(error) {
if (error) {
return res.apiError(error);
}
return res.apiResponse({
'inquiry': inquiry
});
});
};
我遇到的问题是 OPTIONS 请求 404 而不是 returning 200。
即使我手动处理 OPTIONS 请求并且只是 return 状态 200,POST 请求也不会遵循 OPTIONS 请求。我是否误解了 OPTIONS 请求生命周期?
我也试过传递不同的内容类型,如 'application/x-www-form-urlencoded
和 text/plain
以及匹配这些格式的数据,这不会导致 OPTIONS 请求,但 Keystone 没有解析内容其中 POSTs.
我做错了什么?
显然我错过了 Keystone 文档中的部分,您可以在其中自定义 Express 实例然后将其交给 Keystone。
// keystone.js
var keystone = require('keystone');
var express = require('express');
var cons = require('consolidate');
var nunjucks = require('nunjucks');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text());
keystone.app = app;
然后在我的 Aurelia 应用程序中,我在将其发布到 Keystone 之前调用 JSON.stringify(myObject)
。随后在我的 Keystone 视图中,我只需要将字符串解析回 JSON:
exports.post = function(req, res, next) {
if (!req.body) {
return res.sendStatus(400);
}
var data = JSON.parse(req.body),
inquiry = new Inquiry.model(data);
inquiry.save(function(error) {
if (error) {
return res.apiError(error);
}
return res.apiResponse({
'inquiry': inquiry
});
});
};
我在 Keystone 的支持下与 Aurelia 一起构建 SPA。
当从我的 Aurelia 视图中 POST 时,Content-Type
被设置为 application/json
,这当然会在来自客户端时引发 OPTIONS
请求-边.
我的 Keystone init 设置在 cors 上完全开放用于本地开发:
keystone.init({
...
'cors allow origin': true,
'cors allow methods': true,
'cors allow headers': true,
...
});
我的 API Keystone 路由绑定是:
exports = module.exports = function(app) {
app.all('/api/*', keystone.middleware.cors);
app.post('/api/inquiry', keystone.middleware.api, routes.api.inquiries.post);
};
我的 Keystone API 视图是:
var keystone = require('keystone'),
Inquiry = keystone.list('Inquiry');
exports.post = function(req, res) {
var inquiry = new Inquiry.model({
name: {
first: req.body.name.first,
last: req.body.name.last
},
email: req.body.email,
phone: req.body.phone,
question: req.body.question
});
inquiry.save(function(error) {
if (error) {
return res.apiError(error);
}
return res.apiResponse({
'inquiry': inquiry
});
});
};
我遇到的问题是 OPTIONS 请求 404 而不是 returning 200。
即使我手动处理 OPTIONS 请求并且只是 return 状态 200,POST 请求也不会遵循 OPTIONS 请求。我是否误解了 OPTIONS 请求生命周期?
我也试过传递不同的内容类型,如 'application/x-www-form-urlencoded
和 text/plain
以及匹配这些格式的数据,这不会导致 OPTIONS 请求,但 Keystone 没有解析内容其中 POSTs.
我做错了什么?
显然我错过了 Keystone 文档中的部分,您可以在其中自定义 Express 实例然后将其交给 Keystone。
// keystone.js
var keystone = require('keystone');
var express = require('express');
var cons = require('consolidate');
var nunjucks = require('nunjucks');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.text());
keystone.app = app;
然后在我的 Aurelia 应用程序中,我在将其发布到 Keystone 之前调用 JSON.stringify(myObject)
。随后在我的 Keystone 视图中,我只需要将字符串解析回 JSON:
exports.post = function(req, res, next) {
if (!req.body) {
return res.sendStatus(400);
}
var data = JSON.parse(req.body),
inquiry = new Inquiry.model(data);
inquiry.save(function(error) {
if (error) {
return res.apiError(error);
}
return res.apiResponse({
'inquiry': inquiry
});
});
};