Questions tagged [hapi.js]

Hapi is a rich Node.js framework for building web applications and services. It is a simple to use configuration-centric framework with built-in support for input validation, caching, authentication, and other essential facilities.

hapi.js
Filter by
Sorted by
Tagged with
144 votes
9 answers
348k views

How to destroy JWT Tokens on logout?

I am using jwt plugin and strategy in hapijs. I am able to create jwt token while login user and authenticate other API using the same token through 'jwt' strategy. I am setting the token in request....
Garima's user avatar
  • 1,616
138 votes
6 answers
42k views

How do Express and hapi compare to each other?

From web application design and development point of view, how do Express and Hapi compare to each other? For basic examples they seem similar, however I'm interested to learn more about key ...
Ali Shakiba's user avatar
  • 20.8k
115 votes
7 answers
172k views

Joi validation of array

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({ '...
1977's user avatar
  • 2,772
60 votes
7 answers
26k views

How to store routes in separate files when using Hapi?

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 }); ...
Centurion's user avatar
  • 14.2k
59 votes
2 answers
47k views

How to allow any other key in Joi [duplicate]

I have a simple requirement. I tried to search on the internet as well as documentation but failed. So here is what I want to achieve: I have a schema: const schema = Joi.object().keys({ a: Joi....
Anand Undavia's user avatar
49 votes
4 answers
37k views

Joi validation return only one error message

I have a three field form made of a name field, email field and a textarea. I'm using Joi 4.7.0 version along with hapijs. I use the object below validate the input. I receive the data object from an ...
Mazzy's user avatar
  • 13.8k
41 votes
2 answers
36k views

Multipart HTTP response

The goal is for a Node.js / hapi API server to respond to a browser's AJAX request with two things: A media file (e.g. an image) A JSON object with metadata about the file These are two separate ...
Seth Holladay's user avatar
39 votes
3 answers
50k views

Joi validation multiple conditions

I have the following schema: var testSchema = Joi.object().keys({ a: Joi.string(), b: Joi.string(), c: Joi.string().when('a', {'is': 'avalue', then: Joi.string().required()}) }); but I ...
user2468170's user avatar
  • 1,254
32 votes
7 answers
55k views

Joi validator conditional schema

I need to create dynamic schema to validate my api request query in node js using Joi validator depending on a key in the request query. Say the following below mentioned pattern are my valid queries. ...
Nitheesh's user avatar
  • 19.7k
32 votes
1 answer
27k views

Joi object validation: How to validate values with unknown key names?

I have an object with key names I cannot possibly know - they are created by user. However I do know what values they (keys) are going to store, and they (values) are going to be ISO strings. How do I ...
wscourge's user avatar
  • 11k
31 votes
6 answers
29k views

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

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....
Brendan's user avatar
  • 829
26 votes
3 answers
23k views

hapijs joi validation , just validate one field and to allow any field

I want to validate one field and to allow another fields without validation; by example just to validate: "firstname" field. In my code when I comment 'payload', hapi allow me to record any field, ...
stackdave's user avatar
  • 6,855
23 votes
3 answers
18k views

How to log all requests made to a hapi server without using a logging library?

I'd like to see a nice log with short info about each request to my server, for use during development. I've seen the documentation on http://hapijs.com/api#request-logs, but I couldn't understand ...
mik01aj's user avatar
  • 12.1k
23 votes
3 answers
39k views

JOI :allow null values in array

I am trying to add a validation for array in a POST request Joi.array().items(Joi.string()).single().optional() I need to allow null values in the payload. Can you please tell me how this can be ...
user1110790's user avatar
23 votes
5 answers
54k views

JOI email validation

Im using Joi library as standalone validator for my CRA project but when firing email() validator im getting cryptic error Uncaught Error: Built-in TLD list disabled
Konrad Albrecht's user avatar
23 votes
3 answers
45k views

How to deploy backend and frontend projects if they are separate?

I am developing a web application with a small team, and after researching and studying a bit we discovered it is a good practice to separate back-end and front-end projects. So we will develop the ...
Victor Silva Do Nascimento's user avatar
21 votes
1 answer
35k views

How to have Keycloak login page in iframe?

There is a web server running locally, and I want to have Keycloak (on another domain) login page inside the iframe. I tried the following setting in the Keycloak Real Settings > Security Defenses > ...
srgbnd's user avatar
  • 5,504
20 votes
2 answers
30k views

Joi Validation - Compare to dates from POST

I'm currently using Joi in HapiJS / NodeJS to validate data. One POST in particular has two ISO dates (start date and end date) that are passed to the route and validated to make sure they are ISO ...
tdotcspot's user avatar
  • 345
20 votes
5 answers
15k views

How to get the full URL for a request in Hapi

In my hapijs app, given a Request object, how can I find the original, unparsed, unmodified URL? function getRequestUrl (request) { return ...; // What goes here? } I've found that I can piece ...
gilly3's user avatar
  • 89.4k
20 votes
3 answers
3k views

Hapi nested routing

Suppose I want to have REST endpoints which look roughly like this: /projects/ /projects/project_id /projects/project_id/items/ /projects/project_id/items/item_id CRUD on each if makes sense. For ...
PoMaHTuK's user avatar
  • 223
20 votes
3 answers
11k views

How should I use Swagger with Hapi?

I have a working ordinary Hapi application that I'm planning to migrate to Swagger. I installed swagger-node using the official instructions, and chose Hapi when executing 'swagger project create'. ...
Kitanotori's user avatar
  • 1,761
18 votes
8 answers
29k views

can I change the color of log data in winston?

I happened to use bunyan to log the the data . I wanted the logs be printed with appropriate colors like errors in red , debug yellow .. etc; unfortunately I couldn't find anyways to do that . And now ...
srujana's user avatar
  • 501
17 votes
2 answers
25k views

hapi route joi validation of password confirmation

How do I check that password and password_confirmation are the same ? var Joi = require('joi'), S = Joi.string().required().min(3).max(15); exports.create = { payload: { username: S, ...
Whisher's user avatar
  • 32k
17 votes
3 answers
20k views

How to upload files using nodejs and HAPI?

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")....
Realdheeraj's user avatar
17 votes
9 answers
9k views

HapiJS global path prefix

I'm writing an API on HapiJS, and wondering how to get a global prefix. For example, all requests should be made to: https://api.mysite.com/v0/... So I'd like to configure v0 as a global prefix. ...
Tyler's user avatar
  • 11.4k
17 votes
2 answers
27k views

Nodejs Hapi - How to enable cross origin access control

I am working HapiJs Restful web service and trying to enable cors so any client even from different domain can consume my services. I tried cors=true in server connection object but didn't work.
Gunjan Kumar's user avatar
17 votes
2 answers
24k views

Multiple Joi validation types

I search a lot but nothing found to allow multiple type validation in Joi Link: https://github.com/hapijs/joi I would like to use something like this: validate: { type: joi.or([ joi....
Mr.Orange's user avatar
  • 426
16 votes
3 answers
13k views

TypeError: reply is not a function

Using Hapi v17, I am just trying to make a simple web API to start building my knowledge, but I keep getting an error every time I test the GET methods built out. Below is the code I am running: '...
Drew's user avatar
  • 1,351
15 votes
3 answers
8k views

TypeError: server.connection is not a function in Hapi nodejs

I started working with Hapi nodejs framework. I am using "[email protected]" and here is my code in server.js to initiate application. 'use strict'; const Hapi = require('hapi'); const server = new Hapi....
Jitendra's user avatar
  • 3,165
15 votes
4 answers
16k views

Chrome doesn't send cookies after redirect

In node.js (using Hapi framework) I'm creating link for user to allow my app reading user account. Google handles that request and asks about giving permissions. Then Google makes redirect to my ...
Alan Mroczek's user avatar
  • 1,139
15 votes
1 answer
9k views

Best way to maintain only one schema between Mongoose and Joi

I'm using Hapi to develop a web service, with Mongoose as ODM and Joi as validator. I really like Joi's validation and the way it connects with HAPI (I need Joi's description function to display some ...
Manuel Bitto's user avatar
  • 5,163
14 votes
1 answer
24k views

joi validation: Set minimum array length conditionally

I have an array field which i would like to ensure that it has at least one element when a condition is met: genre:Joi.array().includes(data.genres).when('field'{is:'fieldValue',then:Joi.required()}) ...
user2468170's user avatar
  • 1,254
14 votes
1 answer
29k views

Using Joi, validating that a boolean is true

Is it possible to validate that a boolean is true using Joi? I've tried using allow, valid and invalid without any luck.
anthonator's user avatar
  • 5,065
14 votes
1 answer
6k views

Role based authentication in HapiJS

I am working on a rest API first project written with HapiJS. After the login process the user gets a token to pass in the header of every request. Users have different roles (admin, standard, guest, ...
Mino's user avatar
  • 635
14 votes
1 answer
8k views

How can I customize the validation error response in hapi.js?

When using the config.validate option on a route and a request fails due to validation, hapi returns an error like: { "statusCode": 400, "error": "Bad Request", "message": "child \"weight\...
Well Actually's user avatar
14 votes
5 answers
6k views

apollostack/graphql-server - how to get the fields requested in a query from resolver

I am trying to figure out a clean way to work with queries and mongdb projections so I don't have to retrieve excessive information from the database. So assuming I have: // the query type Query { ...
Vikk's user avatar
  • 637
13 votes
7 answers
18k views

HAPI JS Node js creating https server

How do I create a hapi http and https server, listening on both 80 and 443 with the same routing? (I need a server which should run both on http and https with the exact same API)
Sathish's user avatar
  • 2,088
13 votes
2 answers
19k views

hapi.js Cors Pre-flight not returning Access-Control-Allow-Origin header

I have an ajax file upload using (Dropzone js). which sends a file to my hapi server. I realised the browser sends a PREFLIGHT OPTIONS METHOD. but my hapi server seems not to send the right response ...
kweku360's user avatar
  • 1,055
13 votes
3 answers
6k views

How to make Hapi auto reload app during developing

I'm new to *Hapi *framework. During development, I have to restart the hapi server whenever I made any changes to the code. For the view part, I can add an option {isCached: false}to make the view ...
snowery's user avatar
  • 468
13 votes
3 answers
23k views

hapi.js best way to handle errors

I'm creating my first node.js REST web service using hapi.js. I'm curious as to the best way to handle errors let's say from my dao layer. Do i throw them in my dao layer and then just try/catch ...
Catfish's user avatar
  • 19.1k
13 votes
1 answer
39k views

Promise reject Possibly unhandled Error:

I have a function that does some operation using an array. I would like to reject it when the array is empty. As an example myArrayFunction(){ return new Promise(function (resolve, reject) ...
Juan's user avatar
  • 816
13 votes
2 answers
10k views

Hapi.js application architecture

I've been working on a hapi.js application and have the following architecture: Database --Models --usermodel.js --anothermodel.js Routes --private --controllers --ctrl1.js ...
I_Debug_Everything's user avatar
13 votes
5 answers
7k views

How to return an array of errors with graphQL

How can I return multiple error messages like this ? "errors": [ { "message": "first error", "locations": [ { "line": 2, "column": 3 } ], "path": [ ...
Lev's user avatar
  • 14.9k
12 votes
2 answers
19k views

Allow optional parameters in Joi without specifying them

I'm fairly new to using Joi to validate request payloads in hapi. My question is the following. I have this defined route: { method: 'POST', path: '/foo/bar', config: { description: ...
João Minhós Rodrigues's user avatar
12 votes
4 answers
17k views

How can I add a middleware in my route?

In express I have something like this: router.get('/foo', middlewareFunction, function (req, res) { res.send('YoYo'); }); What is the form for a middleware in hapi? When I have this: server....
Antonio Sandoval's user avatar
12 votes
4 answers
8k views

Is there a way to view all routes in a Hapi server

We are working on a node.js Hapi server that pulls in a list of routes from a MongoDB database and sets up said routes for servicing. With this, there is the potential for the server to fail because ...
J2N's user avatar
  • 321
12 votes
4 answers
13k views

How do I override config values at runtime with node-config?

I'd like to override some values at test-time, specifically setting my retries for an http service to 1 (immediate failure, no retries). Our project uses node-config. According to the docs I can ...
jcollum's user avatar
  • 45k
12 votes
1 answer
5k views

how to inject mock testing hapi with Server.inject

I want to test hapi routes with lab, I am using mysql database. The problem using Server.inject to test the route is that i can't mock the database because I am not calling the file that contains the ...
Manan Vaghasiya's user avatar
12 votes
1 answer
3k views

Hapi.js redirect from onRequest

I'm using hapi v6.11.1 and have been trying to conditionally redirect oncoming request from my hapi.js server to another server. This is what I've trie so far: server.on('onRequest',function(request,...
I_Debug_Everything's user avatar
12 votes
0 answers
4k views

Node.js: How do I protect against malicious image file uploads? I.e., how do I implement an image sanitizer in Node?

I am creating a web app using hapi.js that allows users to upload images. I am validating the uploaded images in both the client and server to only allow .jpg/.jpeg, .png, and .gif files. However, I ...
Samuel Earl's user avatar

1
2 3 4 5
28