使用sinch和node.js服务器进行SMS验证

时间:2018-02-21 11:00:48

标签: node.js node-modules sinch sinch-verification

我想在Node.js服务器上实施短信验证,当我向带有用户电话号码的网址发送请求时,我可以向该电话号码发送验证短信,我阅读了文档here,仍然,当我想将sinch导入我的应用程序时出错 像下面的代码

const express        = require('express');
const bodyParser     = require('body-parser');
const app            = express();
const PORT = process.env.PORT || 5000;
const path = require('path');
const fetch = require('node-fetch');
var APPLICATION_KEY = 'MYAPP_KEY' ;
var SinchClient = require('sinch-rtc');

var sinchClient = new SinchClient({
    applicationKey: APPLICATION_KEY,
    capabilities: {messaging: true},
    onLogMessage: function(message) {
        console.log(message);
    }
});

sinchClient.start(CREDENTIALS).then(function() {
        console.log('Success!');
    })




app.use( bodyParser.json() );      // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));


app.use(express.static(path.join(__dirname, 'public')))
  .set('views', path.join(__dirname, 'views'))
  .set('view engine', 'ejs')
  .get('/', (req, res) => res.render('pages/index'))
  .get('/test', (req, res) => {
    //mycodes to verify phone number


  })
.listen(PORT, () => console.log(`Listening on ${ PORT }`));

它会给我这个错误

(function (exports, require, module, __filename, __dirname)
{ function Notification(e,t,i,n){this.progress=e/t,this.message=i,this.object=n}function getBrowserInfo(){var e,t=navigator.userAgent,i=t.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i)||t.match(/(applewebkit(?=\/))\/?\s*(\d+)/i)||[];return/trident/i.test(i[1])?"IE "+((e=/\brv[ :]+(\d+)/g.exec(t)||[])[1]||""):"Chrome"===i[1]&&null!=(e=t.match(/\bOPR\/(\d+)/))?"Opera "+e[1]:(i=i[2]?[i[1],i[2]]:[navigator.appName,navigator.appVersion,"-?"],null!=(e=t.match(/version\/(\d+)/i))&&i.splice(1,1,e[1]),i.join("/").substring(0,50))}function getPlatformInfo(){return navigator.platform}
function Sinch(e){if(!e)throw new TypeError("Could not create SinchClient, configuration not provided.");if(e.capabilities=e.capabilities||{},"string"!=typeof e.applicationKey)throw new TypeError("Could not create SinchClient, applicationKey is not a string");this.c

TypeError: Cannot read property 'indexOf' of undefined
    at new Sinch (C:\Users\user\project\node_modules\sinch-rtc\lib\sinch.node.min.js:1:1741)
    at Object.<anonymous> (C:\Users\user\project\server.js:9:13)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

先谢谢

1 个答案:

答案 0 :(得分:0)

要发送短信,你不应该将消息设置为true,实际上我会将它用于节点 https://www.npmjs.com/package/sinch-verification