Created
February 27, 2020 16:34
-
-
Save yadomi/022ca72631d86473f8645cabcbbfd6c0 to your computer and use it in GitHub Desktop.
Hapi querystring issues
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
{ | |
"dependencies": { | |
"@hapi/hapi": "^19.1.1", | |
"@hapi/joi": "^17.1.0", | |
"qs": "^6.9.1" | |
} | |
} |
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 Hapi = require('@hapi/hapi'); | |
const Joi = require('@hapi/joi'); | |
const QS = require('qs'); | |
const init = async () => { | |
const server = Hapi.server({ | |
port: 3333, | |
host: 'localhost', | |
query: { | |
parser: query => QS.parse(query) // This won't work because query is an object but QS.parse expect a string | |
}, | |
routes: { | |
validate: { | |
failAction: (request, h, err) => { | |
if (err) console.error(err); | |
throw err; | |
} | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/test', | |
handler: (request, h) => { | |
console.log(request.query); // without the options.validate, request.query.status is a string eg: "[a, b]", I'm expecting an array | |
return 'ok'; | |
}, | |
options: { | |
// validate: { | |
// query: Joi.object({ | |
// workspaceId: Joi.string(), | |
// status: Joi.array().items(Joi.string().valid('a', 'b')), | |
// }) | |
// } | |
} | |
}); | |
await server.start(); | |
console.log('Server running on %s', server.info.uri); | |
}; | |
init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment