hapijs

How to capture callback from failed validation in Joi

你。 提交于 2019-12-08 04:54:49
问题 We're building a web service using Hapi. Our routes have some validation. I was wondering if it was possible to capture or override the default callback on failed validation, before or after hapi replies to the client. my (non-working) code: { method: 'GET', config: { tags: tags, validate: { params: { id: Joi.number() .required() .description('id of object you want to get'), }, //Tried this, and it's not working: callback: function(err, value) { if (err) { console.log('need to catch errors

Hapi.js views not seeing local scripts

青春壹個敷衍的年華 提交于 2019-12-07 23:37:12
问题 I'm currently working on an app with a server that uses Hapi, and I am running into a problem whenever I try to load a .jade file. Here is the current code for my index.js file: var Hapi = require('hapi'); var internals = {}; internals.main = function() { var options = { views: { engines: { jade: 'jade' }, path: '../app', compileOptions: { pretty: true } } }; this._server = new Hapi.createServer('localhost', 8000, options); this._server.route({ method: 'GET', path: '/', handler: function

how to get handlebar-layouts working in node/hapi.js

夙愿已清 提交于 2019-12-07 12:18:11
问题 I'm having trouble getting handlebars-layouts to render in a hapi.js app. The layout renders fine but the partial doesn't render at all and just has a blank. I'm declaring it like this: var handlebars = require('handlebars'), layouts = require('handlebars-layouts'); layouts(handlebars); server.views({ engines: { html: handlebars }, basePath: __dirname, path: './views', layoutPath: './views/layout', partialsPath: './views/partials', layout: true, helpersPath: './views/helpers' }); and the

Hapijs Custom 500 Error Page

我们两清 提交于 2019-12-07 06:32:05
问题 Looking through Hapi's documentation, and trying to google, I can find how to setup a 404 page, but I cannot find anything about setting up a 500 page. I tried adding an error handler like follows: server.on('internalError', function (request, err) { console.log("Internal Error:", err); request.reply.view('errors/500', { error: err }).code(500); }); But my hook never gets called. Is there an easy way to return a custom 500 page with Hapijs? 回答1: You need to trap the error response inside an

How to set Flash Messages in Hapi JS?

南笙酒味 提交于 2019-12-06 12:00:40
问题 Well, i tried to use the hapi-flash but it didnt work for me. So any one here using flash messages in Hapi JS 回答1: Yes, you'll want to use Yar. Once you've registered it as a plugin, within each handler you can use: request.session.flash('error', 'There was an error.'); To get the flash message, you use request.session.flash('error'). Which will return all the 'error' messages currently in the flash. It will also clear the flash - specifics can be found on the repo. I find it helpful to us

Hapi.js views not seeing local scripts

假装没事ソ 提交于 2019-12-06 04:40:46
I'm currently working on an app with a server that uses Hapi, and I am running into a problem whenever I try to load a .jade file. Here is the current code for my index.js file: var Hapi = require('hapi'); var internals = {}; internals.main = function() { var options = { views: { engines: { jade: 'jade' }, path: '../app', compileOptions: { pretty: true } } }; this._server = new Hapi.createServer('localhost', 8000, options); this._server.route({ method: 'GET', path: '/', handler: function(request, reply) { reply.view('index') } }); // Start server. this._server.start() }; internals.main(); The

Hapi server on heroku fails to bind port

不问归期 提交于 2019-12-06 03:43:11
问题 I'm working on a Hapi server for a ReactJS app but when I try to deploy to Heroku, I get the R10 error "Failed to bind to $PORT within 60 seconds of launch". What is going on? I'm using process.env.PORT. I also tried parseInt() around it. Also tried disabling varying packages. The build is successful always. In the Heroku logs, I see the console log from the index.js ("Hapi running on ...") but then the R10 error shows up and the server restarts, then crashes. ==> 🌎 Hapi Production Server

How app.listen() and app.get() work on express and hapi

瘦欲@ 提交于 2019-12-06 03:13:46
With http node module (only native modules) how i can recreate app.listen() and app.get() use http module with a constructor var app = function(opts) { this.token= opts.token } app.prototype.get = function(callback) { // use request and response of app.listen() } app.prototype.active = function(callback) { // use request and response of app.listen() // return on callback some manipulate //request params } app.prototype.listen = function() { // start http or https server } Import the modules and work with this var app = require(...) Var client = new app({ token: 0000 }) client.get(function

how to get handlebar-layouts working in node/hapi.js

拥有回忆 提交于 2019-12-05 22:30:35
I'm having trouble getting handlebars-layouts to render in a hapi.js app. The layout renders fine but the partial doesn't render at all and just has a blank. I'm declaring it like this: var handlebars = require('handlebars'), layouts = require('handlebars-layouts'); layouts(handlebars); server.views({ engines: { html: handlebars }, basePath: __dirname, path: './views', layoutPath: './views/layout', partialsPath: './views/partials', layout: true, helpersPath: './views/helpers' }); and the boiler plate layout <html> <body> {{#content "body"}} <h2>Welcome Home</h2> {{/content}} {{#content "foot"}

Hapijs Custom 500 Error Page

核能气质少年 提交于 2019-12-05 11:18:59
Looking through Hapi's documentation, and trying to google, I can find how to setup a 404 page, but I cannot find anything about setting up a 500 page. I tried adding an error handler like follows: server.on('internalError', function (request, err) { console.log("Internal Error:", err); request.reply.view('errors/500', { error: err }).code(500); }); But my hook never gets called. Is there an easy way to return a custom 500 page with Hapijs? You need to trap the error response inside an onPreResponse extension function and set a new HTML response there. The same principle applies to any Boom