2017-08-10 94 views
1

我正在嘗試使用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。 –

回答

1

此客戶端代碼結束了工作:這裏

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); 
}; 
1

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