Skip to content

Instantly share code, notes, and snippets.

@poeticninja
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poeticninja/9e8b2e81c20c9e39bf56 to your computer and use it in GitHub Desktop.
Save poeticninja/9e8b2e81c20c9e39bf56 to your computer and use it in GitHub Desktop.
Base hapi server with logging.
/**
* Dependencies.
*/
var Hapi = require('hapi');
var Good = require('good');
var GoodConsole = require('good-console');
var Boom = require('boom');
var Joi = require('joi');
// Create a new server
var server = new Hapi.Server();
// Setup the server with a host and port
server.connection({
port: parseInt(process.env.PORT, 10) || 3000,
host: '0.0.0.0',
router: {
stripTrailingSlash: true
}
});
server.route({
method: 'GET',
path: '/{path}',
config: {
handler: function(request, reply){
reply(Boom.notFound());
}
}
});
/*
Load all plugins and then start the server.
First: community/npm plugins are loaded
Second: project specific plugins are loaded
*/
server.register([
{
register: Good,
options: {
reporters: [{
reporter: GoodConsole,
events: { ops: '*', request: '*', log: '*', response: '*', 'error': '*' }
}]
}
}
], function () {
//Start the server
server.start(function() {
//Log to the console the host and port info
console.log('Server started at: ' + server.info.uri);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment