hapijs

Hapijs using both Http and Https on one connection

大憨熊 提交于 2019-12-03 08:50:35
New to Hapijs and trying to use it to create an app that uses HTTPS for all requests and redirect HTTP to the secure connection.The problem is the app goes in HTTPS mode no problem but if i change the URL to HTTP the server does not respond and don't know the reason why. This is what i have came up with so far, it works but not for HTTP var connectionOptions = { port: 3000, tls: { key: fs.readFileSync(path.join(__dirname, 'key/key.pem'), 'utf8'), cert: fs.readFileSync(path.join(__dirname, 'key/cert.pem'), 'utf8') } }; var server = new Hapi.Server(); server.connection(connectionOptions); //This

Joi validation of array

折月煮酒 提交于 2019-12-03 08:12:45
问题 trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :( validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string()

Joi validation of array

元气小坏坏 提交于 2019-12-03 02:04:09
trying to validate that an array has zero or more strings in one case and that it has zero or more objects in another , struggling with Joi docs :( validate: { headers: Joi.object({ 'content-type': "application/vnd.api+json", accept: "application/vnd.api+json" }).options({ allowUnknown: true }), payload : Joi.object().keys({ data : Joi.object().keys({ type: Joi.any().allow('BY_TEMPLATE').required(), attributes: Joi.object({ to : Joi.string().email().required(), templateId : Joi.string().required(), categories : Joi.array().items( //trying to validate here that each element is a string),

How to store routes in separate files when using Hapi?

本小妞迷上赌 提交于 2019-12-02 14:17:20
All of the Hapi examples (and similar in Express) shows routes are defined in the starting file: var Hapi = require('hapi'); var server = new Hapi.Server(); server.connection({ port: 8000 }); server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply('Hello, world!'); } }); server.route({ method: 'GET', path: '/{name}', handler: function (request, reply) { reply('Hello, ' + encodeURIComponent(request.params.name) + '!'); } }); server.start(function () { console.log('Server running at:', server.info.uri); }); However, it's not hard to image how large this file can grow

How to reply from outside of the hapi.js route handler

喜你入骨 提交于 2019-12-02 05:21:56
问题 I have a hapi.js route where I want to defer the response. I've tried storing the reply function and calling it later, or wrapping it in a promise, but hapi always responds immediately with a 500 Internal Server Error response. Store the reply for later: var pendingReplies = {}; server.route({ method: "POST", path: "/", handler: function (request, reply) { var id = generateId(); pendingReplies[id] = reply; } }); ... // reply later by calling: function sendResponse(id, data) { pendingReplies

How to reply from outside of the hapi.js route handler

孤街醉人 提交于 2019-12-02 02:26:45
I have a hapi.js route where I want to defer the response. I've tried storing the reply function and calling it later, or wrapping it in a promise, but hapi always responds immediately with a 500 Internal Server Error response. Store the reply for later: var pendingReplies = {}; server.route({ method: "POST", path: "/", handler: function (request, reply) { var id = generateId(); pendingReplies[id] = reply; } }); ... // reply later by calling: function sendResponse(id, data) { pendingReplies[id](data); } I've tried creating a promise to reply: handler: function (request, reply) { var id =

Using Joi, require one of two fields to be non empty

℡╲_俬逩灬. 提交于 2019-12-01 02:54:38
If I have two fields, I'd just like to validate when at least one field is a non empty string, but fail when both fields are empty strings. Something like this does not validate var schema = Joi.object().keys({ a: Joi.string(), b: Joi.string() }).or('a', 'b'); When validating against {a: 'aa', b: ''} The or condition only tests for the presence of either key a or b , but does test whether the condition for a or b is true. Joi.string() will fail for empty strings. Here is gist with some test cases to demonstrate http://requirebin.com/?gist=84c49d8b81025ce68cfb Code below worked for me. I used

Using Joi, require one of two fields to be non empty

拜拜、爱过 提交于 2019-11-30 22:53:03
问题 If I have two fields, I'd just like to validate when at least one field is a non empty string, but fail when both fields are empty strings. Something like this does not validate var schema = Joi.object().keys({ a: Joi.string(), b: Joi.string() }).or('a', 'b'); When validating against {a: 'aa', b: ''} The or condition only tests for the presence of either key a or b , but does test whether the condition for a or b is true. Joi.string() will fail for empty strings. Here is gist with some test

nodejs hapi single page

戏子无情 提交于 2019-11-30 08:58:49
I have a single app site (NodeJS) and I want to migrate from Express to Hapi, what I normally do is serve static files and route everything else to a single page which contains the angularjs app and the angular routing configuration. // Express routing, first the static files app.use( express.static(__dirname + '/public') ); // Second the api routes app.get('/api', function(req, res){ res.send( {api: 'response' } ) }); // Finally everything else maps to the single page app: app.get('*', function(req, res){ res.sendfile('./public/html/controllers.index.html') }); In HapiJS I dont know how to

How to upload files using nodejs and HAPI?

本小妞迷上赌 提交于 2019-11-30 03:17:32
Can anyone tell me How to upload files Using nodejs and HAPI? I am getting binary data inside the handler. Here is my html code: function sendFormFromHTML(form) { //form = $(".uploadForm").form; var formData = new FormData(form); formData.append('id', '123456'); // alternative to hidden fields var xhr = new XMLHttpRequest(); xhr.open('POST', form.action, true); xhr.onload = function(e) { alert(this.responseText) }; xhr.send(formData); return false; } <form method="post" id="uploadForm" action="http://localhost:3000/api/uploadfiles" enctype="multipart/form-data"> <label for="upload">File