-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (55 loc) · 1.79 KB
/
server.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
const express = require('express')
const app = express()
const axios = require('axios')
const path = require('path')
const linkify = require('linkifyjs')
const async = require('async')
require('dotenv').config()
const PORT = process.env.PORT || 8080
const KEY = process.env.KEY
const SECRET = process.env.SECRET
app.use(express.static(__dirname + '/dist'), express.static(__dirname + '/assets'))
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.get('/send-message', (req, res) => {
const urls = linkify.find(req.query.message)
async.eachSeries(urls, function (url, done) {
axios({
url: 'https://api-ssl.bitly.com/shorten',
params: {
access_token: process.env.BITLY_TOKEN,
longUrl: url.href
}
}).then(function (response) {
req.query.message = req.query.message.replace(url.value, response.data.results[url.href].shortUrl)
done(null)
})
.catch(function (error) {
console.log(error)
done(error)
})
}, function (err) {
console.log(err)
axios({
url: 'https://api.transmitsms.com/send-sms.json',
withCredentials: true,
auth: {
username: KEY,
password: SECRET
},
params: {
to: req.query.to,
message: req.query.message
}
}).then(function (response) {
res.send(response)
console.log(response)
})
.catch(function (error) {
res.send(error)
console.log(error)
})
})
})
app.listen(PORT, () => console.log('App listening on port' + PORT + '!'))