从 Node.JS 回调函数启用 CORS
Enable CORS from a Node.JS Callback Function
我正在尝试使用 Twilio 函数来处理我的 Twilio 应用程序的令牌生成。我以前使用 Node.js + Express Server 来完成此操作,但我不知道如何在这种类型的环境中启用 CORS。
我的客户端代码如下所示:
$('#new-twilio').click(function(){
var toNum = this.value;
if(token == undefined) {
$.getJSON('https://my-twilio-function/endpoint').done(function(data){
token = data.token;
Twilio.Device.setup(token, {debug: true});
Twilio.Device.ready(function(device){
Twilio.Device.connect({"PhoneNumber": toNum});
});
}).fail(function(error){
alert("Failure!");
alert(JSON.stringify(error));
});
} else {
Twilio.Device.connect({"PhoneNumber": toNum});
}
});
我的函数代码如下所示:
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
const ClientCapability = require('twilio').jwt.ClientCapability;
const responseHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST",
"Access-Control-Allow-Headers": "content-type, accept",
"Content-Type": "application/json"
};
let identity = "sampleIdentity";
const capability = new ClientCapability({
accountSid: context.ACCOUNT_SID,
authToken: context.AUTH_TOKEN
});
capability.addScope(new ClientCapability.IncomingClientScope(identity));
capability.addScope(new ClientCapability.OutgoingClientScope({
applicationSid: context.TWILIO_TWIML_APP_SID
}));
console.log(capability.toJwt());
callback(null, {headers: responseHeaders, identity: identity, token: capability.toJwt()});
};
值得注意的是,console.log 证明此函数正在返回我需要的确切标记,但我继续收到此错误:
XMLHttpRequest cannot load https://my-twilio-function/endpoint. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
显然,我的 twilio 函数是真实的 URL。和我一样google,我找不到如何允许对这种类型的节点方法进行访问控制。
此客户端代码最终运行:
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
const ClientCapability = require('twilio').jwt.ClientCapability;
const response = new Twilio.Response();
response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
response.appendHeader('Content-Type', 'application/json');
let identity = "sampleIdentity";
const capability = new ClientCapability({
accountSid: context.ACCOUNT_SID,
authToken: context.AUTH_TOKEN
});
capability.addScope(new ClientCapability.IncomingClientScope(identity));
capability.addScope(new ClientCapability.OutgoingClientScope({
applicationSid: context.TWILIO_TWIML_APP_SID
}));
response.setBody({identity: identity, token: capability.toJwt()})
console.log(capability.toJwt());
callback(null, response);
};
这里是 Twilio 开发人员布道者。
很高兴看到 K. Rhoda 解决了这个问题。我只是想弄清楚是什么让它起作用。
有一个 custom response 您可以从函数中的 Twilio.Response
访问。响应初始化如下:
const response = new Twilio.Response();
然后有以下有用的方法:
// set the body of the response
response.setBody(body);
// set a header for the response
response.appendHeader(key, value);
// set all the headers for the response
response.setHeaders(obj);
// set the status code for the response
response.setStatusCode(200);
然后您可以使用回调函数发送该响应,如下所示:
callback(null, response);
在我的例子中,上面的所有内容都不起作用,因为我在我的功能中检查了“检查有效的 Twilio 签名”(默认情况下)并提出了没有签名的请求。
在我取消选中后,上面的答案有效。所以请注意你是否勾选了这个,如果勾选了,你的请求是否有正确的签名。
我正在尝试使用 Twilio 函数来处理我的 Twilio 应用程序的令牌生成。我以前使用 Node.js + Express Server 来完成此操作,但我不知道如何在这种类型的环境中启用 CORS。
我的客户端代码如下所示:
$('#new-twilio').click(function(){
var toNum = this.value;
if(token == undefined) {
$.getJSON('https://my-twilio-function/endpoint').done(function(data){
token = data.token;
Twilio.Device.setup(token, {debug: true});
Twilio.Device.ready(function(device){
Twilio.Device.connect({"PhoneNumber": toNum});
});
}).fail(function(error){
alert("Failure!");
alert(JSON.stringify(error));
});
} else {
Twilio.Device.connect({"PhoneNumber": toNum});
}
});
我的函数代码如下所示:
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
const ClientCapability = require('twilio').jwt.ClientCapability;
const responseHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST",
"Access-Control-Allow-Headers": "content-type, accept",
"Content-Type": "application/json"
};
let identity = "sampleIdentity";
const capability = new ClientCapability({
accountSid: context.ACCOUNT_SID,
authToken: context.AUTH_TOKEN
});
capability.addScope(new ClientCapability.IncomingClientScope(identity));
capability.addScope(new ClientCapability.OutgoingClientScope({
applicationSid: context.TWILIO_TWIML_APP_SID
}));
console.log(capability.toJwt());
callback(null, {headers: responseHeaders, identity: identity, token: capability.toJwt()});
};
值得注意的是,console.log 证明此函数正在返回我需要的确切标记,但我继续收到此错误:
XMLHttpRequest cannot load https://my-twilio-function/endpoint. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
显然,我的 twilio 函数是真实的 URL。和我一样google,我找不到如何允许对这种类型的节点方法进行访问控制。
此客户端代码最终运行:
exports.handler = function(context, event, callback) {
const client = context.getTwilioClient();
const ClientCapability = require('twilio').jwt.ClientCapability;
const response = new Twilio.Response();
response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
response.appendHeader('Content-Type', 'application/json');
let identity = "sampleIdentity";
const capability = new ClientCapability({
accountSid: context.ACCOUNT_SID,
authToken: context.AUTH_TOKEN
});
capability.addScope(new ClientCapability.IncomingClientScope(identity));
capability.addScope(new ClientCapability.OutgoingClientScope({
applicationSid: context.TWILIO_TWIML_APP_SID
}));
response.setBody({identity: identity, token: capability.toJwt()})
console.log(capability.toJwt());
callback(null, response);
};
这里是 Twilio 开发人员布道者。
很高兴看到 K. Rhoda 解决了这个问题。我只是想弄清楚是什么让它起作用。
有一个 custom response 您可以从函数中的 Twilio.Response
访问。响应初始化如下:
const response = new Twilio.Response();
然后有以下有用的方法:
// set the body of the response
response.setBody(body);
// set a header for the response
response.appendHeader(key, value);
// set all the headers for the response
response.setHeaders(obj);
// set the status code for the response
response.setStatusCode(200);
然后您可以使用回调函数发送该响应,如下所示:
callback(null, response);
在我的例子中,上面的所有内容都不起作用,因为我在我的功能中检查了“检查有效的 Twilio 签名”(默认情况下)并提出了没有签名的请求。
在我取消选中后,上面的答案有效。所以请注意你是否勾选了这个,如果勾选了,你的请求是否有正确的签名。