Text has been sent to ' + data.number + '
'}) //catching smsStatus from app.js//create send functionfunction send() {const number = numberInput.value.replace(/\D/g, '');const text = textInput.value;//make post request with this data using Fetch API || this needs to be "catched" on the server side why additional code is written to app.jsfetch('/', { method: 'post', headers: {'Content-Type': 'application/json'},//turn this into json string body: JSON.stringify({ number: number, text: text})})//fetch api uses promises so we need to use then/catch to get a response.then(function(res){console.log(res);}).catch(function(err){console.log(err);});}app.jsconst express = require('express');const bodyParser = require('body-parser');const ejs = require('ejs');const Nexmo = require('nexmo');const socketio = require('socket.io');//Init NEXMOconst nexmo = new Nexmo({ apiKey: 'mkey', apiSecret: 'mysecret'}, {debug: true});//Init appconst app = express();//engine setupapp.set('view engine', 'html');app.engine('html', ejs.renderFile);//public folder client side javascriptapp.use(express.static(__dirname + '/public'));//bodyParser middlewareapp.use(bodyParser.json());app.use(bodyParser.urlencoded( { extended: true}));//index route (before this state, when opened localhost:4000, the message was 'Cannot GET')app.get('/', (req, res) => { res.render('index');})//Catch submitted FORM from HTMLapp.post('/', (req, res) => {//change values that come from the form into variablesconst number = req.body.number;const text = req.body.text;const from = 'nexmo';nexmo.message.sendSms(from, number, text, {tpye: 'unicode'},(err, responseData) => {if(err) {console.log(err);} else {console.dir(responseData);//Get data from the responseDataconst data = { id: responseData.messages[0]['message-id'], number: responseData.messages[0]['to']}//Emit the responseData to the client to display a live information via socket.io || catch on client side is happening in main.js io.emit('smsStatus', data);}});});//define Portconst port = 4000;//start serverconst server = app.listen(port, () => console.log('Server started on port ${port}'));//Connect to Socket.ioconst io = socketio(server);io.on('connection', (socket) => {console.log('Connected');io.on('disconnect', () => {console.log('Disconnected');})})//Socket.io Adavanced notifications. Confirmation is not only displayed in the innerHTMLbut as an actual browser notification//Step 1 request Permission to notifiy//Step 2 creating function that passes data via socket.io function displayStatus(message) {var notification = new Notification('Nexmo', { body: message//icon: 'images/XX.png'});}index.html
SMS Versand
Submitted February 16, 2020 at 08:37PM by Ok-Suggestion
No comments:
Post a Comment