forked from TwilioDevEd/survey-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (46 loc) · 1.42 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
var path = require('path');
var express = require('express');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var config = require('./config');
var voice = require('./routes/voice');
var message = require('./routes/message');
var bullhorn = require('./routes/bullhorn');
var results = require('./routes/results');
var Promise = require('bluebird');
// use node A+ promises
mongoose.Promise = Promise;
// check for connection string
if (!config.mongoUrl) {
throw new Error('MONGO_URL env variable not set.');
}
var isConn;
// initialize MongoDB connection
if (mongoose.connections.length === 0) {
mongoose.connect(config.mongoUrl);
} else {
mongoose.connections.forEach(function(conn) {
if (!conn.host) {
isConn = false;
}
})
if (isConn === false) {
mongoose.connect(config.mongoUrl);
}
}
// Create Express web app with some useful middleware
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan('combined'));
// Twilio Webhook routes
app.post('/voice', voice.interview);
app.post('/voice/:responseId/transcribe/:questionIndex', voice.transcription);
app.post('/message', message);
//Bullhorn Webhook
app.post('/bullhorn', bullhorn);
// Ajax route to aggregate response data for the UI
app.get('/results', results);
module.exports = app;