由于某种奇怪的原因,错误消息没有显示在终端中,而出现在邮递员中。我正在使用joi
验证程序。请让我知道我做错了什么!谢谢!
const Joi = require("joi");
const express = require("express");
app = express();
app.use(express.json());
const courses= [
{ id:1, name:"course1"},
{ id:2, name:"course2"},
{ id:3, name:"course3"}
]
app.get("/", function(req,res) {
res.send("Hello Worlds");
});
app.get("/api/courses", function(req,res){
res.send(courses);
});
app.get("/api/courses/:id", function(req,res) {
const course =courses.find(c=>c.id===parseInt(req.params.id))
if(!course) res.status(404).send("Course with given id was not found...");
res.send(course);
});
app.post("/api/courses", function(req,res) {
const schema = {
name: Joi.string().min(3).required()
};
const result = Joi.validate(req.body, schema);
if (result.error ) {
res.status(400).send(result.error.details[0].message);
return;
}
const course= {
id: courses.length +1,
name:req.body.name
};
courses.push(course);
res.send(course);
})
Port = process.env.Port || 2000;
app.listen(Port, console.log(`Listening on port ${Port}`));
下面是我要在终端中显示的消息键的路径:
"isJoi": true,
"name": "ValidationError",
"details": [
{
"message": "\"name\" length must be at least 3 characters long",
"path": [
"name"
],
"type": "string.min",
"context": {
"limit": 3,
"value": "r",
"key": "name",
"label": "name"
}
}
],
"_object": {
"name": "r"
}