Carolina Pascale Campos
Intern at Codeminer42
Student at UFSCar
A simple API using Node.js, express and mongo. Its purpose is to illustrate how you can organize a RESTful application.
/characters
JSON Response
{
"data": [
{
"_id": "581c8c0e8ae4dbf68e92e178",
"name": "Batman",
"ocupation": "CEO of Wayne Enterprises",
"quote": "As a symbol I can be incorruptible. I can be everlasting.",
"__v": 0,
"firstAppearance": 1939
},
{
"_id": "581cb70702eec407ff7414dd",
"name": "Joker",
"ocupation": "Villain",
"quote": "Why so serious?",
"__v": 0,
"firstAppearance": 1940
}
]
}
How to create custom errors with mongoose
charactersModel.js
const minFirstAppearance = [1939, 'The first appearance year should be greater than 1939'];
const charactersSchema = mongoSchema({
firstAppearance: { type: Number, min: minFirstAppearance, default: 1939}
});
Request
{
"name": "James Gordon",
"ocupation": "Police Commissioner",
"quote": "Because he's not our hero.",
"firstAppearance": 1938
}
Response
{
"error": {
"type": "ValidationError",
"message": "characters validation failed",
"description": {
"firstAppearance": "The first appearance year should be greater than 1939"
}
}
}
Thanks for your attention!
Any questions?