-
-
Save mcortesi/744efd25b721d08b187afed75b2b5472 to your computer and use it in GitHub Desktop.
Hapi.js & Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const newrelic = require('newrelic'); | |
const Hapi = require('hapi'); | |
const Boom = require('boom'); | |
const BPromise = require('bluebird'); | |
const server = new Hapi.Server({ | |
useDomains: true, | |
debug: false, | |
connections: { | |
routes: { | |
timeout: { | |
server: 10 * 1000 | |
} | |
} | |
} | |
}); | |
server.connection({ port: 8111 }); | |
server.route({ | |
method: 'GET', | |
path: '/handler/ok', | |
handler: function (request, reply) { | |
reply('Everything is good!'); | |
} | |
}); | |
// Works OK | |
server.route({ | |
method: 'GET', | |
path: '/handler/sync-throw', | |
handler: (request, reply) => { | |
throw new Error("Sync Throw"); | |
} | |
}); | |
// Works OK | |
server.route({ | |
method: 'GET', | |
path: '/handler/async-throw', | |
handler: function (request, reply) { | |
setTimeout(() => { throw new Error("Async Throw") }, 10); | |
} | |
}); | |
// No Log, No response | |
server.route({ | |
method: 'GET', | |
path: '/handler/promise-throw', | |
config: { | |
timeout: { server: 1000 } | |
}, | |
handler: function (request, reply) { | |
return BPromise.delay(10).then(() => { | |
throw new Error('Promise Throw') | |
}) | |
} | |
}); | |
// Works Ok | |
server.route({ | |
method: 'GET', | |
path: '/ext/sync-throw', | |
config: { | |
ext: { | |
onPreHandler: { | |
method: (request, reply) => { | |
throw new Error("Sync Throw"); | |
} | |
} | |
} | |
}, | |
handler: function (request, reply) { | |
reply('Everything is good!'); | |
} | |
}); | |
// Works Ok | |
server.route({ | |
method: 'GET', | |
path: '/ext/async-throw', | |
config: { | |
ext: { | |
onPreHandler: { | |
method: (request, reply) => { | |
setTimeout(() => { throw new Error("Async Throw") }, 10); | |
} | |
} | |
} | |
}, | |
handler: function (request, reply) { | |
reply('Everything is good!'); | |
} | |
}); | |
// No Log, No response | |
server.route({ | |
method: 'GET', | |
path: '/ext/promise-throw', | |
config: { | |
// timeout: { server: 1000 }, | |
ext: { | |
onPreHandler: { | |
method: (request, reply) => { | |
BPromise.delay(10).then(() => { throw new Error('Promise Throw') }); | |
} | |
} | |
} | |
}, | |
handler: function (request, reply) { | |
reply('Everything is good!'); | |
} | |
}); | |
// No Log, No response | |
server.route({ | |
method: 'GET', | |
path: '/ext/promise-reply-continue', | |
config: { | |
timeout: { server: 1000 }, | |
ext: { | |
onPreHandler: { | |
method: (request, reply) => { | |
BPromise.delay(10).then(() => { | |
reply.continue(); | |
}) | |
} | |
} | |
} | |
}, | |
handler: function (request, reply) { | |
throw new Error("Handler has thrown"); | |
} | |
}); | |
// Works Ok | |
server.route({ | |
method: 'GET', | |
path: '/ext/promise-as-callback-reply-continue', | |
config: { | |
timeout: { server: 1000 }, | |
ext: { | |
onPreHandler: { | |
method: (request, reply) => { | |
BPromise.delay(10).asCallback((err) => { | |
reply.continue(); | |
}); | |
} | |
} | |
} | |
}, | |
handler: function (request, reply) { | |
throw new Error("Handler has thrown"); | |
} | |
}); | |
// Works Ok | |
server.route({ | |
method: 'GET', | |
path: '/handler/reply-error', | |
handler: function (request, reply) { | |
reply(new Error("Handler has thrown")); | |
} | |
}); | |
const goodOptions = { | |
ops: { interval: 1000 }, | |
reporters: { | |
console: [ | |
{ | |
module: 'good-squeeze', | |
name: 'Squeeze', | |
args: [{ log: '*', response: '*'}] | |
}, { | |
module: 'good-console' | |
}, | |
'stdout' | |
], | |
} | |
} | |
server.on('log', (event, tags) => { | |
console.log('logEvent: ', event); | |
}); | |
server.on('request-error', (request, error) => { | |
console.log('request-error: ', error); | |
}); | |
server.register({ register: require('good'), options: goodOptions }, (err) => { | |
if (err) { | |
return console.error(err); | |
} | |
server.start(err => { | |
if (err) { | |
throw err; | |
} | |
console.log('Server running at:', server.info.uri); | |
}); | |
}); | |
module.exports = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment