Rest API example to practice
Create a Express server that handles:
- A
GET
request endpoint at/
as a landing page for your API. - A
GET
request endpoint at/user
to display all users in DB. - A
POST
request endpoint at/user
to add new user to DB. - A
PUT
request endpoint at/user/:name
to update user from DB upon their name. - A
PATCH
request endpoint at/user/:name
to update some user data from DB upon their name. - A
GET
request endpoint at/display/:name
to display one user from DB upon their name.
New user endpoint should be able to accept a JSON object like the following:
{
"userName": "steel",
"userPass": "123pass",
"age": "32",
"fbw": "48",
"toolStack": ["Js", "Html5", "Css3", "Sass"],
"email": "[email protected]"
}
- Create a middleware method that will make sure the object received contains
userName
,userPass
,age
,fbw
andemail
. - Create a middleware method that will check if the user is above 18 years old
- Create a middleware method that will check if the user belongs to our FBW
- If all the above is true, then you should send a response with a success message
- If any of the middleware fails, you should send a response with an error message that says why the user is not valid.
// Success case
{
"message" : "This user is valid!"
}
// Failure
{
"message": "We can not validate your user. They are not a member of FBW48"
}
// Other case of Failure
{
"message": "We can not validate your user. we don't accept pp that are below 18 years of age"
}
- Create a middleware that makes the
firstName
starts with a capital letter. - Create a middleware that sorts the
toolStack
array alphabetically. - Create a middleware that will turn
age
andfbw
to numbers.
{
"userName": "Steel",
"userPass": "123pass",
"age": 32,
"fbw": 48,
"toolStack": ["Css3", "Html5", "Js", "Sass"],
"email": "[email protected]"
}
Make sure that you Organize (route, controller and module) Ps: new our new Rest API needs new DB ๐
Happy codding โ๏ธ