Skip to content

Instantly share code, notes, and snippets.

@leftieFriele
Created July 1, 2014 20:25
Show Gist options
  • Save leftieFriele/26015f2dc17f0ecfe72d to your computer and use it in GitHub Desktop.
Save leftieFriele/26015f2dc17f0ecfe72d to your computer and use it in GitHub Desktop.
Configuring TLS for Hapi using Convict
/// This is just a simple sample of how you can easily use Convict for configuration of server options for TLS in a node web app
// config.js
var convict = require('convict');
var conf = convict({
env: {
doc: 'Application enviroments',
format: ['production', 'development'],
default: 'development',
env: 'NODE_ENV'
},
port: {
doc: 'Port to bind',
format: 'port',
default: 8000,
env: 'PORT'
},
serverOptions: {
doc: 'Hapi server options to pass in',
format: Object,
default: {}
}
});
var env = conf.get('env');
conf.loadFile('./config/' + env + '.json');
conf.validate();
module.exports = conf;
// production.json
{
"tls": true
}
// development.json
{
"tls": false
}
// In your application code, do something like this
var hapi = require('hapi');
var config = require('./config');
if (config.get('tls')){
console.log('configuring TLS');
config.load({
serverOptions: {
tls: {
ca: fs.readFileSync('ca.pem'),
key: fs.readFileSync('liggeran-ssl-private.key'),
cert: fs.readFileSync('liggeran-ssl.crt')
}
}
});
}
var server = Hapi.createServer('', config.get('port'), config.get('serverOptions'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment