从Node.JS回调函数启用CORS

问题描述:

我正在尝试使用Twilio函数为我的Twilio应用程序处理令牌生成。我以前使用Node.js + Express服务器来完成这个任务,但我不知道如何在这种类型的环境中找出启用CORS的方法。
我的客户端代码如下所示:从Node.JS回调函数启用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。尽我谷歌,我无法找到如何允许访问控制这种类型的节点方法。

+0

它会看看你导入那个函数的地方,以及什么'回调函数'有趣吗? – adeneo

+0

这有帮助吗? https://www.twilio.com/docs/api/runtime/functions/faq#how-do-i-send-cors-headers我不禁注意到他们没有提及对“*”的支持(或缺乏它)。 – Will

+0

@adeneo,回调应该返回一个数据到任何被称为函数的地方。如果它是一个JavaScript对象,它将作为JSON返回,你也可以做TwiML。 –

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

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

有一个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); 
+0

我可以建议你(假设Twilio开发者传道人意味着与公司的某种连接)创建令牌生成模板?我认为这是一个常见的用例。 –

+0

我真的为Twilio工作:)这是一个很好的建议,并且它与CORS一起工作是其中的一个重要部分。我实际上已经开始了我自己的[有用函数](https://github.com/philnash/useful-twilio-functions/)的回购,并且要完成令牌生成(欢迎PR);)。我们在平台上工作时也纳入了更多模板。感谢您的反馈! – philnash