March 18, 20215 yr hello friends, Still new to node I was follwing a tutorial on how touse Joi9 validation with node express but i am getting this error here is my code app.js const express = require('express'); const path = require('path'); const Joi = require('joi'); const bodyParser = require('body-parser'); const app = express(); //const hostname = '127.0.0.1'; app.use('/public',express.static(path.join(__dirname,'static'))); app.use(bodyParser.urlencoded({extended:false})); app.use(bodyParser.json()); //const port = 3000; app.get('/',(req,res)=>{ res.sendFile(path.join(__dirname,'static','index.html')); }); app.post('/',(req,res)=>{ console.log(req.body); const schema = Joi.object().keys({ email: Joi.string().trim().email().required(), password: Joi.string().min(5).max(10).required() }); Joi.validate(req.body,schema,(err,result)=>{ if(err){ console.log(err); res.send('an error occured'); } console.log(result); res.send('succesfully posted data'); }) }); app.listen(3000); and index.html inside static folder <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Html</title> <style> /* Forms */ [type=text], [type=email], [type=url], [type=password], select, textarea { display: block; padding: .5rem; background: transparent; vertical-align: middle; width: 100%; max-width: 100%; border: 1px solid #cdcdcd; border-radius: 4px; font-size: .95rem; } [type=text]:focus, [type=email]:focus, [type=url]:focus, [type=password]:focus, select:focus, textarea:focus { outline: none; border: 1px solid #1E6BD6; } select { -webkit-appearance: none; -moz-appearance: none; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAJCAYAAAA/33wPAAAAvklEQVQoFY2QMQqEMBBFv7ERa/EMXkGw11K8QbDXzuN4BHv7QO6ifUgj7v4UAdlVM8Uwf+b9YZJISnlqrfEUZVlinucnBGKaJgghbiHOyLyFKIoCbdvecpyReYvo/Ma2bajrGtbaC58kCdZ1RZ7nl/4/4d5EsO/7nzl7IUtodBexMMagaRrs+06JLMvcNWmaOv2W/C/TMAyD58dxROgSmvxFFMdxoOs6lliWBXEcuzokXRbRoJRyvqqqQvye+QDMDz1D6yuj9wAAAABJRU5ErkJggg==) 100% no-repeat; line-height: 1 } label { font-weight: 600; font-size: .9rem; display: block; margin: .5rem 0; } /* Other */ * { box-sizing: border-box; } html { -webkit-font-smoothing: antialiased; padding: 1rem; } .container { max-width: 600px; margin: 0 auto; padding: 0 1rem; } /* Button */ [type=submit] { display: inline-block; vertical-align: middle; white-space: nowrap; cursor: pointer; margin: .25rem 0; padding: .5rem 1rem; border: 1px solid #1E6BD6; border-radius: 18px; background: #1E6BD6; color: white; font-weight: 600; text-decoration: none; font-family: sans-serif; font-size: .95rem; margin-top:1rem; } </style> </head> <body> <div class="container"> <form action="/" method="POST" id="form"> <label for="email">Email</label> <input type="email" id="email" placeholder="Email Address"> <label for="password">Password</label> <input type="password" id="password" placeholder="Password"> <input type="submit" value="Submit"> </form> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script> </script> </body> </html>
March 18, 20215 yr You are also better off testing servers using a tool like Insomnia or postman rather than a HTML file then you can narrow down where your problem is. Going off the Joi docs `validate()` isn't a function that's directly attached to the module. You need to create a schema and then validate against that see the first example here https://joi.dev/api/?v=17.4.0 const schema = Joi.object({ username: Joi.string() .alphanum() .min(3) .max(30) .required(), }) schema.validate({ username: 'abc' }); // -> { value: { username: 'abc' } } schema.validate({}); // -> { value: {}, error: '"username" is required' } I personally prefer using Zod for validation https://www.npmjs.com/package/zod Edited March 18, 20215 yr by rbrtsmith
Create an account or sign in to comment