A simple todo api using node.js and mongodb with JWT Authentication.
Setup
To get all the requred packaged run:
npm install
In order to start the API run:
nodemon source/server.ts
Registers a new user
-
URL
/api/users/register
-
Method:
POST
-
Data Params
username=[string]
password=[string]
-
Success Response:
- Code: 200
Content:{ user : { username: "admin", password: "password" } }
- Code: 200
-
Error Response:
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 500 INTERNAL SERVER ERROR
-
Sample Call:
axios.post('/api/users/register', { username: 'admin', password: 'password' }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });
Logs in a user in the system
-
URL
/api/users/login
-
Method:
POST
-
Data Params
username=[string]
password=[string]
-
Success Response:
- Code: 200
Content:{ user : { username: "admin", password: "password" }, token: "JWT" }
- Code: 200
-
Error Response:
- Code: 401 UNAUTHORIZED
Content:{ error : "Wrong username or password" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 401 UNAUTHORIZED
-
Sample Call:
axios.post('/api/users/login', { username: 'admin', password: 'password' }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });
Gets all todos
-
URL
/api/todos/get
-
Method:
GET
-
Data Params
None
-
Headers
Authentication = "Bearer " + JWT
-
Success Response:
- Code: 200
Content:{ todos: [ { _id: "exmaple", title: "Learn Node.js", isCompleted: false }, { _id: "exmaple2", title: "Do the dishes", isCompleted: false } ] }
- Code: 200
-
Error Response:
- Code: 401 UNAUTHORIZED
Content:{ error : "Unauthorized" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 401 UNAUTHORIZED
-
Sample Call:
axios.get('/api/todos/get') .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });
Creates a new Todo
-
URL
/api/todos/create
-
Method:
POST
-
Data Params
title=[string]
-
Headers
Authentication = "Bearer " + JWT
-
Success Response:
- Code: 200
Content:{ todo: { _id: "exmaple", title: "Learn Node.js", isCompleted: false } }
- Code: 200
-
Error Response:
- Code: 401 UNAUTHORIZED
Content:{ error : "Unauthorized" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 401 UNAUTHORIZED
-
Sample Call:
axios.post('/api/todos/create' { title: 'Learn Node.js' }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });
Updates an exsisting Todo
-
URL
/api/todos/update
-
Method:
PATCH
-
Data Params
_id=[string]
title=[string]
isCompleted=[boolean]
-
Headers
Authentication = "Bearer " + JWT
-
Success Response:
- Code: 200
Content:{ todo: { _id: "id", title: "Updated title", isCompleted: true } }
- Code: 200
-
Error Response:
- Code: 404 NOT FOUND
Content:{ error : "Record not found" }
OR
- Code: 401 UNAUTHORIZED
Content:{ error : "Unauthorized" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 404 NOT FOUND
-
Sample Call:
axios.post('/api/todos/update' { _id: 'id', title: 'Updated title', isCompleted: true }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });
Delete a Todo
-
URL
/api/todos/delete
-
Method:
DELETE+
-
Data Params
_id=[string]
-
Headers
Authentication = "Bearer " + JWT
-
Success Response:
- Code: 200
Content:{ todo : { _id: "id", title: "Updated title", isCompleted: true } }
- Code: 200
-
Error Response:
- Code: 404 NOT FOUND
Content:{ error : "Record not found" }
OR
- Code: 401 UNAUTHORIZED
Content:{ error : "Unauthorized" }
OR
- Code: 500 INTERNAL SERVER ERROR
Content:{ error : "Internal error" }
- Code: 404 NOT FOUND
-
Sample Call:
axios.post('/api/todos/delete' { _id: '_id' }) .then((response) => { console.log(response); }) .catch((error) => { console.log(error); });