从节点.JS回调函数启用 CORS



我正在尝试使用 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()});

};
值得注意的是,控制台.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。尽管我用谷歌搜索,但我找不到如何允许对这种类型的节点方法进行访问控制。

这个客户端代码最终工作了:

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解决了这个问题。我只是想弄清楚是什么让它工作。

您可以从函数中的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签名"(默认情况下(并在没有签名的情况下发出请求。

在我取消选中它后,上面的答案起作用了。因此,请注意您是否已检查此内容,如果是,则注意您的请求是否有正确的签名。

最新更新