To get the Node server running locally:
- Clone this repo
npm install
to install all required dependencies- Install MongoDB Community Edition and run it
node server.js
to start the local server
- expressjs
- jsonwebtoken
- mongoose
- morgan
- bcrypt
- bcrypt.js
- body-parser
- cors
- dotenv
In backend/middleware
, a middleware was added as an error handler for Mongoose's validation errors.
After a user is registered, a token will be given when they login. Requests are authenticated by adding x-auth-token
with its token as a value in the header with a valid JWT.
It was my first time working with authentication. I used bcrypt to apply a salt and hash the passwords.
It was also my first time working with a database, and I decided to use MongoDB for this application. The folder models/
contains the schema definitions for the Mongoose models.
Users must be registered and logged in to edit or delete a task. Users may also search for and access tasks by id, username, or the status of completion.
- Register new user
method: "POST"
url: "/api/register"
body: {
"username": <String>,
"password": <String>,
}
- User Login
method: "POST"
url: "/api/login"
body: {
"username": <String>,
"password": <String>
}
- Validate token
method: "POST"
url: "/api/tokenIsValid"
header: {
key: x-auth-token,
value: <token given at login>
}
- View all todo tasks: GET /api/todos
method: "GET"
url: "/api/todos"
- View tasks by username
method: "GET"
url: "/api/todos/username/:username"
- View tasks by id
method: "GET"
url: "/api/todos/:id"
-
View tasks by status of completion
- Completed tasks:
method: "GET" url: "/api/todos/isCompleted/true"
- Incompleted tasks:
method: "GET" url: "/api/todos/isCompleted/false"
- Completed tasks:
-
Add/create a new task
method: "POST"
url: "/api/todos/"
body: {
"username": <String>,
"task": <String>,
"isCompleted": <Boolean>
}
- Edit/update a task by id: after getting token validated
method: "PUT"
url: "/api/todos/:id"
body: {
"username": <String>,
"task": <String>,
"isCompleted": <Boolean>
}
- Edit/update task by username: after gettting token validated
method: "PUT"
url: "/api/todos/username/:username"
body: {
"username": <String>,
"task": <String>,
"isCompleted": <Boolean>
}
- Delete a task: after getting token validated
method: "DELETE"
url: "/api/todos/:id"
I did attempt to incorporate GraphQL with a schema and resolver, but since MongoDB and authentication were both new to me already, I eventually took out GraphQL to ensure the rest of it was working.
Overall, as someone who was always more comfortable with the front-end, it felt good for me to learn more and practice working with the back-end through this exercise. I hope I didn't miss too many points with this! Thank you for reviewing.