-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathapp.js
74 lines (62 loc) · 1.65 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const Nexmo = require('nexmo');
const socketio = require('socket.io');
// Init Nexmo
const nexmo = new Nexmo({
apiKey: 'YOURAPIKEY',
apiSecret: 'YOURAPISECRET'
}, { debug: true });
// Init app
const app = express();
// Template engine setup
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
// Public folder setup
app.use(express.static(__dirname + '/public'));
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Index route
app.get('/', (req, res) => {
res.render('index');
});
// Catch form submit
app.post('/', (req, res) => {
// res.send(req.body);
// console.log(req.body);
const { number, text } = req.body;
nexmo.message.sendSms(
'YOURVURTUALNUMBER', number, text, { type: 'unicode' },
(err, responseData) => {
if(err) {
console.log(err);
} else {
const { messages } = responseData;
const { ['message-id']: id, ['to']: number, ['error-text']: error } = messages[0];
console.dir(responseData);
// Get data from response
const data = {
id,
number,
error
};
// Emit to the client
io.emit('smsStatus', data);
}
}
);
});
// Define port
const port = 3000;
// Start server
const server = app.listen(port, () => console.log(`Server started on port ${port}`));
// Connect to socket.io
const io = socketio(server);
io.on('connection', (socket) => {
console.log('Connected');
io.on('disconnect', () => {
console.log('Disconnected');
})
});