var express = require('express'); var alexa = require('alexa-app'); var bodyParser = require('body-parser'); const dbURL = 'mongodb://localhost:27017/tellall'; const PORT = 5000; var MongoClient = require ('mongodb').MongoClient; var ObjectID = require ('mongodb').ObjectID; const router = express.Router (); router.get ('/intents', async (req, res, next) => { try { const id = req.params.id; db.collection ('intent_list').find({}).toArray((err,result)=>{ if (err) throw err; res.json(result); }); } catch (e) { console.log ('error:', e); next (e); } }); router.get ('/deleteIntent/:id', async (req, res, next) => { try { let id = req.params.id; let result = db.collection('intent_list').remove({_id: ObjectID(id)},(err,result)=>{ if (err) throw err; res.json(result); }); } catch (e) { console.log ('error:', e); next (e); } }); router.post ('/updateIntent/:id', async (req, res, next) => { try { let id = req.params.id; let intent = req.body; delete intent._id; let result = db.collection('intent_list').update({_id: ObjectID(id)}, intent,{upsert:true}, (err, result)=>{ if (err) throw(err); res.json(result); }); } catch (e) { console.log ('error:', e); next (e); } }); var app = express(); // ALWAYS setup the alexa app and attach it to express before anything else. var alexaApp = new alexa.app('step3'); alexaApp.express({ expressApp: app, // verifies requests come from amazon alexa. Must be enabled for production. // You can disable this if you're running a dev environment and want to POST // things to test behavior. enabled by default. checkCert: false, // sets up a GET route when set to true. This is handy for testing in // development, but not recommended for production. disabled by default debug: true }); // now POST calls to /test in express will be handled by the app.request() function // from here on you can setup any other express routes or middlewares as normal alexaApp.launch(function(request, response) { response.say("You launched Saburly app!"); }); alexaApp.request = (jsonRequest) => { const alexaRequest = new alexa.request(jsonRequest); if (alexaRequest.type() === "IntentRequest") { const intent = db.collection('intent_list').findOne({ name: alexaRequest.data.request.intent.name }); if (intent) { const response = new alexa.response(alexaRequest.getSession()); return response.say(intent.answer); } } }; app.use (function (req, res, next) { res.header ('Access-Control-Allow-Origin', '*'); res.header ( 'Access-Control-Allow-Headers', 'Origin, Content-Type' ); res.header ('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); res.header ('Access-Control-Allow-Credentials', 'true'); next (); }); app.use (bodyParser.json ()); app.use ('/', router); MongoClient.connect (dbURL).then (database => { db = database; db.collection ('intent_list'); app.listen (PORT, () => console.log ('Express server running on port ' + PORT) ); });