我正在构建一个网络应用程序,允许用户输入电话号码并通过Twilio API发送短信。我已在文件中构建了该功能,如下所示。如果我cd
到此文件并运行node twilioActions.js
,则会发送短信。
var client = require('twilio')(CLIENT_ID, CLIENT_SECRET);
// ideally, I'd like to send the message using this method and call this from other JavaScript files
export const sendMessage = () => {
}
// the following code works when I run 'node twilioActions.js'
client.sendMessage({
to:'...',
from: '...',
body: 'Text message test.'
}, function(err, responseData) {
if (!err) {
console.log(responseData.from); // outputs "+14506667788"
console.log(responseData.body); // outputs "word to your mother."
}
});
但是,我想从另一个React文件中调用sendMessage
方法。这是它:
import * as twilioActions from './twilioActions';
class PhoneView extends React.Component{
// other methods are hidden obviously, the one below is called when a button is pressed to send a message.
sendMessage() {
twilioActions.sendMessage();
}
}
当我尝试构建项目时,出现以下错误:
ERROR in ./~/twilio/package.json
Module parse failed:/Users/Felix/Desktop/ECE590/node_modules/twilio/package.json Line 2: Unexpected token :
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| "twilio",
@ ./~/twilio/lib/Client.js 5:17-43
ERROR in ./~/request/lib/har.js
Module not found: Error: Cannot resolve module 'fs' in /Users/Felix/Desktop/ECE590/node_modules/request/lib
@ ./~/request/lib/har.js 3:9-22
ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Cannot resolve module 'net' in /Users/Felix/Desktop/ECE590/node_modules/tunnel-agent
@ ./~/tunnel-agent/index.js 3:10-24
ERROR in ./~/tunnel-agent/index.js
Module not found: Error: Cannot resolve module 'tls' in /Users/Felix/Desktop/ECE590/node_modules/tunnel-agent
@ ./~/tunnel-agent/index.js 4:10-24
我觉得我犯了一个简单的错误,也许我没有使用正确的库或包含正确的引用。有人能指出我正确的方向如何让这个工作?非常感谢你!
答案 0 :(得分:0)
Twilio开发者传道者在这里。
twilio npm模块未构建或建议用于前端。主要的是您需要在站点的前端代码中公开您的帐户凭据。这是一种安全风险,因为这意味着恶意攻击者可以获取您的凭据并滥用您的Twilio帐户。
我建议您在服务器端创建一个可以使用AJAX请求调用的服务,以便执行这样的操作。