从Node.JScallback函数启用CORS

我试图使用Twilio函数来处理我的Twilio应用程序的令牌生成。 我以前使用Node.js + Express服务器来完成这一点,但我不知道如何找出在这种types的环境中启用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}); } }); 

我的function代码如下所示:

 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.logcertificate这个函数正在返回我需要的确切的标记,但是我继续得到这个错误:

 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。 就像我谷歌,我无法find如何允许访问控制这种types的节点的方法。

这个客户端代码结束了工作:

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

有一个自定义的响应,您可以从Twilio.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函数发送该响应,如下所示:

 callback(null, response);