Skip to content
This repository was archived by the owner on May 22, 2023. It is now read-only.

Add CORS headers #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
npm-debug.log
.env
.idea
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (process.env.NODE_ENV !== 'test') {
}

// HTTP Port to run our web application
cfg.port = process.env.PORT || 3000;
cfg.port = process.env.PORT || 3003;

// Your Twilio account SID and auth token, both found at:
// https://www.twilio.com/user/account
Expand Down
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@ const bodyParser = require('body-parser');

const router = require('./src/router');

const config = require('./config');

// Create Express webapp
const app = express();

// Add headers
app.all('/*', function(req, res, next) {
// CORS headers
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, Access-Control-Allow-Origin');
res.header("Access-Control-Max-Age", "86400"); // 24 hours

// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
}
else {
next();
}
});


app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended: false}));

app.use(router);

// Create http server and run it
const server = http.createServer(app);
const port = process.env.PORT || 3000;

server.listen(port, function() {
console.log('Express server running on *:' + port);
server.listen(config.port, function() {
console.log('Express server running on *:' + config.port);
});
12 changes: 11 additions & 1 deletion src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const VoiceResponse = require('twilio').twiml.VoiceResponse;
const nameGenerator = require('../name_generator');
const config = require('../config');

const identity = nameGenerator();

exports.tokenGenerator = function tokenGenerator() {
const identity = nameGenerator();
const capability = new ClientCapability({
accountSid: config.accountSid,
authToken: config.authToken,
Expand Down Expand Up @@ -44,6 +45,15 @@ exports.voiceResponse = function voiceResponse(toNumber) {
return twiml.toString();
};

exports.voiceIncoming = function voiceResponse() {
// Create a TwiML voice response
const response = new VoiceResponse();
const dial = response.dial();
dial.client(identity);
console.log(response.toString())
return response.toString();
};

/**
* Checks if the given value is valid as phone number
* @param {Number|String} number
Expand Down
7 changes: 6 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Router = require('express').Router;

const {tokenGenerator, voiceResponse} = require('./handler');
const {tokenGenerator, voiceResponse, voiceIncoming} = require('./handler');

const router = new Router();

Expand All @@ -17,4 +17,9 @@ router.post('/voice', (req, res) => {
res.send(voiceResponse(req.body.To));
});

router.post('/incoming', (req, res) => {
res.set('Content-Type', 'text/xml');
res.send(voiceIncoming());
});

module.exports = router;