Skip to content

Instantly share code, notes, and snippets.

@oprog
Last active February 27, 2019 09:49
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oprog/f7761f9c8034c0ee276b01233dd9a6b7 to your computer and use it in GitHub Desktop.
Save oprog/f7761f9c8034c0ee276b01233dd9a6b7 to your computer and use it in GitHub Desktop.
alexa-sdk how to use it with an express https server
'use strict';
const express = require('express');
const https = require('https');
const fs = require('fs');
const bodyParser = require('body-parser');
const context = require('aws-lambda-mock-context');
// lambda.js contains the lambda function for Alexa as in https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs
var lambda = require('./lambda');
const SERVER_PORT = 443;
const SERVER_IP = <YOUR_IP>;
// SSL Certificate stuff for https
var privateKey = fs.readFileSync(<PATH TO YOUR privateKey file>, 'utf8');
var certificate = fs.readFileSync(<PATH TO YOUR certificate file>, 'utf8');
var ca = fs.readFileSync(<PATH TO YOUR crt file>).toString();
var credentials = {key: privateKey, cert: certificate,ca:ca};
const app = express();
app.use(bodyParser.json({ type: 'application/json' }));
// your service will be available on <YOUR_IP>/alexa
app.post('/alexa/', function (req, res) {
var ctx = context();
lambda.handler(req.body,ctx);
ctx.Promise
.then(resp => { return res.status(200).json(resp); })
.catch(err => { console.log(err);//add your error handling stuff })
});
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(SERVER_PORT, SERVER_IP,function () {
console.log('Alexa Skill service ready on ' + SERVER_IP+":"+SERVER_PORT+" via https. Be happy!");
});
@martinblitz
Copy link

This is exactly what I was looking for! Thanks!!!

@pascalwhoop
Copy link

great!

@jdramosf
Copy link

When you created the configuration file, for the DNS.1 field did you use 'localhost'?

@marchaos
Copy link

Thanks. REST_PORT should be SERVER_PORT.

@oprog
Copy link
Author

oprog commented Jan 30, 2018

@marchaos Right. Fixed, thanks.

@indolent-developer
Copy link

hi can you give me where to find this ./lambda function not able to find in link

@gospodima
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment