Wow I just spent the entire day trying to decipher form validation using Joi, as per usual the documentation was difficult to decipher. There were no example using forms and routes that were easy. In the end I got something like this.
Code:
const Joi = require('joi');
const userSchema = {
name: Joi.number(),
author:Joi.string()
}
server.route({
method: 'POST',
path: '/hello/{name}',
handler: function (request, h) {
//get the $_POST of name
// const payload = request.payload
// return payload.name
const result = Joi.validate({ name: '1',author:''}, userSchema)
var str = result.error;
console.log(str)
if(str === null)
{
return ('Success')
}
else
{
return(str.message)
}
//do something with error
}
Next on the list, is probably file uploading using multr, I will leave sessions to last.
I am using hapi-auto-route to auto load routes from another script
Code:
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"handlebars": "^4.0.12",
"hapi": "^17.5.4",
"hapi-auth-basic": "^5.0.0",
"hapi-auto-route": "^2.0.3",
"inert": "^5.1.0",
"joi": "^13.6.0",
"vision": "^5.4.0"
}
}