-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
66 lines (54 loc) · 1.49 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
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const socketio = require('socket.io');
const africastalking = require('africastalking');
// Init africastalking
const AfricasTalking = new africastalking({
apiKey: 'xxxx',
username: 'sandbox'
}, {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 to = req.body.number;
const message = req.body.text;
const sms = AfricasTalking.SMS;
sms.send({to: `+254${to}`, message})
.then(success => console.log({message: message, to: to}))
.catch(error => console.log(error));
// Get data from response
const data = {
message: message,
number: to
}
// Emit to the client
io.emit('smsStatus', data);
});
// Define port
const port = 8080;
// Start server
const server = app.listen(port, () => console.log(`We Love Nerds ${port}`));
// Connect to socket.io
const io = socketio(server);
io.on('connection', (socket) => {
console.log('Connected');
io.on('disconnect', () => {
console.log('Disconnected');
})
})