有没有一种方法可以通过内部url在同一移动服务上的表拦截器脚本中调用自定义api脚本?Azure移动服务服务到服务通信
还是你总是要使用公共网址(https://.azure-mobile.net)。在这种情况下,与X-ZUMO-MASTER标题一起,因为它是服务通信的服务。自定义API只能从该脚本中调用,而不能由外部应用程序或经过身份验证的用户调用。我想防止主密钥甚至通过加密通道离开服务器。
有没有一种方法可以通过内部url在同一移动服务上的表拦截器脚本中调用自定义api脚本?Azure移动服务服务到服务通信
还是你总是要使用公共网址(https://.azure-mobile.net)。在这种情况下,与X-ZUMO-MASTER标题一起,因为它是服务通信的服务。自定义API只能从该脚本中调用,而不能由外部应用程序或经过身份验证的用户调用。我想防止主密钥甚至通过加密通道离开服务器。
如果您位于不同的服务中,那么您需要使用公共URL,并将您想要调用的API标记为“Admin”访问,如您所述。
如果您想从同一服务中的表脚本调用定制API,那么您可以“定制”自定义API并将其作为常规JS函数调用。请注意,虽然API采用了“请求”和“响应”参数,但这是JavaScript,因此看起来像请求/响应的任何内容都可以工作(鸭子打字)。例如,如果我有这样的所谓的“计算器”定义的自定义API如下:
exports.post = function(request, response) {
var x = request.body.x || request.param('x');
var y = request.body.y || request.param('y');
var op = request.body.op || request.body.operation || request.param('op');
calculateAndReturn(x, y, op, response);
};
exports.get = function(request, response) {
var x = request.param('x');
var y = request.param('y');
var op = request.param('op') || request.param('operator');
calculateAndReturn(x, y, op);
};
function calculateAndReturn(x, y, operator, response) {
var result = calculate(x, y, operator);
if (typeof result === 'undefined') {
response.send(400, { error: 'Invalid or missing parameters' });
} else {
response.send(statusCodes.OK, { result : result });
}
}
function calculate(x, y, operator) {
var undef = {}.a;
if (_isUndefined(x) || _isUndefined(y) || _isUndefined(operator)) {
return undef;
}
switch (operator) {
case '+':
case 'add':
return x + y;
case '-':
case 'sub':
return x - y;
case '*':
case 'mul':
return x * y;
case '/':
case 'div':
return x/y;
}
return undef;
}
function _isUndefined(x) {
return typeof x === 'undefined';
}
注意,对于POST操作,它只从请求需要一个“体”参数具有三个成员(X,Y, op),并且被调用的响应中唯一的功能是send
。我们可以通过将其需要的东西传递给计算器来从表格脚本中调用它:
function insert(item, user, request) {
var calculator = require('../api/calculator');
var quantity = item.quantity;
var unitPrice = item.unitPrice;
calculator.post({ body: { x: quantity, y: unitPrice, op: '*' } }, {
send: function(status, body) {
if (status === statusCodes.OK) {
item.totalPrice = body.result;
request.execute();
} else {
request.respond(status, body);
}
}
});
}