This is a To Do Node API using Express with a MongoDB database and Jest for testing.
Prerequisites:
- You must have node installed on your machine.
- You must have MongoDb installed with a database named
todos
and a collection namedtodos
. - Use the json data in
todos.json
to populate thetodos
collection.
Get the API running:
- Clone this repo.
- Run
npm install
in the terminal from the root of the project. - Run
nodemon start.js
in the terminal from the root of the project. - The API should now be running on localhost:8000.
- Use any of the routes detailed below to test and use the API. The Postman App is very useful for experimenting with API endpoints.
Running the Tests
Run npm run test
Returns json data detailing all of the todos in the todos
collection.
-
URL
/todos
-
Method:
GET
-
URL Params
Required:
None
-
Data Params
None
-
Success Response:
Content:
{ "msg": "relevant tasks displayed", "data": [ { "_id": "[OBJECT ID]", "task": "[TODO NAME]", "details": "[TODO DETAILS]", "completed": [BOOLEAN], "priority": "[HIGH / MEDIUM / LOW]" }, { "_id": "[OBJECT ID]", "task": "[TODO NAME]", "details": "[TODO DETAILS]", "completed": [BOOLEAN], "priority": "[HIGH / MEDIUM / LOW]" } ], "status": 200 }
Returns json data detailing all of the todos in the todos
collection that are either completed/not completed/high, medium or low priority (and combinations of those).
- URL
/todos?completed=true
/todos?completed=false
/todos?priority=low
/todos?priority=medium
/todos?priority=high
-
Method:
GET
-
Success Response:
Content:
{ "msg": "relevant tasks displayed", "data": [ { "_id": "[OBJECT ID]", "task": "[TODO NAME]", "details": "[TODO DETAILS]", "completed": [BOOLEAN], "priority": "[HIGH / MEDIUM / LOW]" }, { "_id": "[OBJECT ID]", "task": "[TODO NAME]", "details": "[TODO DETAILS]", "completed": [BOOLEAN], "priority": "[HIGH / MEDIUM / LOW]" } ], "status": 200 }
Creates new document in the todos collection, returns json data with success message.Validates input via a bespoke REGEX via a validate service.
-
URL
/todos
-
Method:
POST
-
URL Params
Required:
None
-
Data Params
{"task":"take over the world","details":"world domination","priority":"high"}
"task": The task name (string which is validated via the validation service)
"details": The task details (string which is validated via the validation service)
"priority": Can be rated as high, medium or low priority.
All new tasks are automatically assigned a completed status of false when instantiated.
-
Success Response:
Content:{ "msg": "task successfully added", "data": [], "status": 200 }
-
Error Response:
Content:
{
"msg": "did not pass validation, task not added",
"data": [],
"status": 406
}
Returns json data about a specific todo via the URL request
- URL
/todos/:id
- Method:
GET
- URL Params
Required:
Object _id
-
Data Params
None
-
Success Response:
Content:
{
"msg": "task retrieved",
"data": [
{
"_id": "[OBJET ID]",
"task": "[TODO NAME]",
"details": "[TODO DETAILS]",
"priority": "[HIGH / MEDIUM / LOW]",
"completed": [BOOLEAN]
}
],
"status": 200
}
- Error Response:
Content:
{
"msg": "It has failed validation",
"data": [],
"status": 406
}
Gives the user the ablity to edit any property of the todo. Validates user input and success/error messages sent as appropriate
- URL
/todos
- Method:
POST
- URL Params
Required:
None
-
Data Params
Task, details, priority, completed - all or some can be provided by the user
- Success Response:
Content:
{ "msg": "its worked and updated your record", "data": [], "status": 200 }
Deletes document from databse via object id
- URL
/todos
- Method:
DELETE
- URL Params
Required:
None
-
Data Params
Object _id
-
Success Response:
Content:
{
"msg": "task deleted successfully",
"data": [],
"status": 200
}