forked from twilio/starter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
702 changed files
with
1,128 additions
and
60,945 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
node_modules | ||
|
||
lib-cov | ||
*.seed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,92 @@ | ||
// Load app dependencies | ||
var http = require('http'), | ||
path = require('path'), | ||
express = require('express'), | ||
twilio = require('twilio'); | ||
var createError = require('http-errors'); | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var logger = require('morgan'); | ||
var twilio = require('twilio'); | ||
|
||
// Load configuration information from system environment variables. | ||
var TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID, | ||
TWILIO_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN, | ||
TWILIO_NUMBER = process.env.TWILIO_NUMBER; | ||
TWILIO_PHONE_NUMBER = process.env.TWILIO_PHONE_NUMBER; | ||
|
||
// Create an authenticated client to access the Twilio REST API | ||
var client = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN); | ||
|
||
// Create an Express web application with some basic configuration | ||
var app = express(); | ||
|
||
app.configure(function(){ | ||
app.set('port', process.env.PORT || 3000); | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.favicon()); | ||
app.use(express.logger('dev')); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(app.router); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
}); | ||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'ejs'); | ||
|
||
app.configure('development', function(){ | ||
app.use(express.errorHandler()); | ||
}); | ||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// render our home page | ||
app.get('/', function(request, response) { | ||
response.render('index'); | ||
app.get('/', function(req, res, next) { | ||
res.render('index'); | ||
}); | ||
|
||
// handle a POST request to send a text message. This is sent via ajax on our | ||
// home page | ||
app.post('/message', function(request, response) { | ||
// Use the REST client to send a text message | ||
client.sendSms({ | ||
to: request.param('to'), | ||
from: TWILIO_NUMBER, | ||
body: 'Good luck on your Twilio quest!' | ||
}, function(err, data) { | ||
// When we get a response from Twilio, respond to the HTTP POST request | ||
response.send('Message is inbound!'); | ||
}); | ||
// handle a POST request to send a text message. | ||
// This is sent via ajax on our home page | ||
app.post('/message', function(req, res, next) { | ||
// Use the REST client to send a text message | ||
client.messages.create({ | ||
to: req.body.to, | ||
from: TWILIO_PHONE_NUMBER, | ||
body: 'Good luck on your Twilio quest!' | ||
}).then(function(message) { | ||
// When we get a response from Twilio, respond to the HTTP POST request | ||
res.send('Message is inbound!'); | ||
}); | ||
}); | ||
|
||
// handle a POST request to make an outbound call. This is sent via ajax on our | ||
// home page | ||
app.post('/call', function(request, response) { | ||
// Use the REST client to send a text message | ||
client.makeCall({ | ||
to: request.param('to'), | ||
from: TWILIO_NUMBER, | ||
url: 'http://twimlets.com/message?Message%5B0%5D=http://demo.kevinwhinnery.com/audio/zelda.mp3' | ||
}, function(err, data) { | ||
// When we get a response from Twilio, respond to the HTTP POST request | ||
response.send('Call incoming!'); | ||
}); | ||
// handle a POST request to make an outbound call. | ||
// This is sent via ajax on our home page | ||
app.post('/call', function(req, res, next) { | ||
// Use the REST client to send a text message | ||
client.calls.create({ | ||
to: req.body.to, | ||
from: TWILIO_PHONE_NUMBER, | ||
url: 'http://demo.twilio.com/docs/voice.xml' | ||
}).then(function(message) { | ||
// When we get a response from Twilio, respond to the HTTP POST request | ||
res.send('Call incoming!'); | ||
}); | ||
}); | ||
|
||
// Create a TwiML document to provide instructions for an outbound call | ||
app.get('/hello', function(request, response) { | ||
// Create a TwiML generator | ||
var twiml = new twilio.TwimlResponse(); | ||
twiml.say('Hello there! You have successfully configured a web hook.'); | ||
twiml.say('Good luck on your Twilio quest!', { | ||
voice:'woman' | ||
}); | ||
app.get('/hello', function(req, res, next) { | ||
// Create a TwiML generator | ||
var twiml = new twilio.twiml.VoiceResponse(); | ||
// var twiml = new twilio.TwimlResponse(); | ||
twiml.say('Hello there! You have successfully configured a web hook.'); | ||
twiml.say('Good luck on your Twilio quest!', { | ||
voice:'woman' | ||
}); | ||
|
||
// Return an XML response to this request | ||
res.set('Content-Type','text/xml'); | ||
res.send(twiml.toString()); | ||
}); | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function(req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use(function(err, req, res, next) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
|
||
// Return an XML response to this request | ||
response.set('Content-Type','text/xml'); | ||
response.send(twiml.toString()); | ||
// render the error page | ||
res.status(err.status || 500); | ||
res.render('error'); | ||
}); | ||
|
||
// Start our express app, by default on port 3000 | ||
http.createServer(app).listen(app.get('port'), function(){ | ||
console.log("Express server listening on port " + app.get('port')); | ||
}); | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var app = require('../app'); | ||
var debug = require('debug')('starter-node:server'); | ||
var http = require('http'); | ||
|
||
/** | ||
* Get port from environment and store in Express. | ||
*/ | ||
|
||
var port = normalizePort(process.env.PORT || '3000'); | ||
app.set('port', port); | ||
|
||
/** | ||
* Create HTTP server. | ||
*/ | ||
|
||
var server = http.createServer(app); | ||
|
||
/** | ||
* Listen on provided port, on all network interfaces. | ||
*/ | ||
|
||
server.listen(port); | ||
server.on('error', onError); | ||
server.on('listening', onListening); | ||
|
||
/** | ||
* Normalize a port into a number, string, or false. | ||
*/ | ||
|
||
function normalizePort(val) { | ||
var port = parseInt(val, 10); | ||
|
||
if (isNaN(port)) { | ||
// named pipe | ||
return val; | ||
} | ||
|
||
if (port >= 0) { | ||
// port number | ||
return port; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "error" event. | ||
*/ | ||
|
||
function onError(error) { | ||
if (error.syscall !== 'listen') { | ||
throw error; | ||
} | ||
|
||
var bind = typeof port === 'string' | ||
? 'Pipe ' + port | ||
: 'Port ' + port; | ||
|
||
// handle specific listen errors with friendly messages | ||
switch (error.code) { | ||
case 'EACCES': | ||
console.error(bind + ' requires elevated privileges'); | ||
process.exit(1); | ||
break; | ||
case 'EADDRINUSE': | ||
console.error(bind + ' is already in use'); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Event listener for HTTP server "listening" event. | ||
*/ | ||
|
||
function onListening() { | ||
var addr = server.address(); | ||
var bind = typeof addr === 'string' | ||
? 'pipe ' + addr | ||
: 'port ' + addr.port; | ||
debug('Listening on ' + bind); | ||
} |
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.