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
1,363
questions
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....
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 ...
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({
'...
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 });
...
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....
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 ...
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 ...
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 ...
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.
...
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 ...
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....
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, ...
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 ...
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 ...
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
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 ...
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 > ...
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 ...
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 ...
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 ...
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'. ...
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 ...
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,
...
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")....
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. ...
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.
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....
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:
'...
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....
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 ...
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 ...
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()})
...
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.
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, ...
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\...
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 {
...
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)
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 ...
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 ...
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 ...
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) ...
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
...
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": [
...
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: ...
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....
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 ...
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 ...
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 ...
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,...
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 ...