Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dfb213a
Created notes, started with endpoints and installed dependencies.
solen80a May 28, 2025
30afbdc
Added query param for messages added today, /messagesfromtoday
solen80a May 29, 2025
b638bd4
Connected the root to listEndpoints and moved my html documentation t…
solen80a May 30, 2025
3746263
Changed from messages to thoughts
solen80a Jun 4, 2025
06e6057
Installed and imported mongoose
solen80a Jun 4, 2025
dafb7fc
Added Url and Schema
solen80a Jun 4, 2025
e790ebd
Seeded the DB with data from json. Started to transform the code to f…
solen80a Jun 4, 2025
744fa8b
Endpoint /thoughts with query params updated to fetch from API
solen80a Jun 4, 2025
3df9184
Updated endpoint /thoughts/:id to fetch from DB.
solen80a Jun 4, 2025
45c8320
Some cleaning.
solen80a Jun 4, 2025
dfde63d
Added a POST for new thoughts.
solen80a Jun 4, 2025
c3d4609
Added a DELETE for endpoint /thoughts/:id.
solen80a Jun 4, 2025
c938b18
Added PATCH for new thougth message and like.
solen80a Jun 4, 2025
e44a2ae
Added POSt to increase likes to a thought. Also added message to a li…
solen80a Jun 5, 2025
e209250
Changed the response on my get /thoughts
solen80a Jun 5, 2025
3c662ce
Note update
solen80a Jun 6, 2025
1b80d42
Deployed and connected API and DB. Installed dotenv and created a env…
solen80a Jun 10, 2025
b2d38fc
Added bcrypt. Created models thoughts and users
solen80a Jun 10, 2025
03c4556
Updated patch
solen80a Jun 10, 2025
7b7a74e
Updates to patch
solen80a Jun 10, 2025
2ec257a
Made som comments.
solen80a Jun 11, 2025
6df9d70
Broke out models for user and thought. Created get post for users and…
solen80a Jun 11, 2025
d2509ad
Broke out authanticateuser and created routes for thoughts and user, …
solen80a Jun 13, 2025
09dd7fd
Made som cleaning of unused code.
solen80a Jun 15, 2025
fac38d2
Added to include user for edit, delete, post/get thought.
solen80a Jun 16, 2025
427b13b
More fixes to POST thought to identified user.
solen80a Jun 16, 2025
e903905
More authentication fixes.
solen80a Jun 16, 2025
e71a4fd
more fixes
solen80a Jun 16, 2025
cfeda6d
Cleaning and checking tasks.
solen80a Jun 16, 2025
5097a6d
Update deployed frontend and backend.
solen80a Jun 17, 2025
e25664b
Fixed user already exist error handling.
solen80a Sep 3, 2025
21f3752
Removed unused code. Added maxlength to message. Added await to the u…
solen80a Sep 3, 2025
55b2a19
Added check for password length.
solen80a Sep 3, 2025
ccdc4ef
removed empty files.
solen80a Sep 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Install dependencies with `npm install`, then start the server by running `npm r
## View it live

Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about.
https://happy-thoughts-happy-mind.netlify.app/
https://js-project-api-afon.onrender.com/

6 changes: 3 additions & 3 deletions data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"_id": "682bab8c12155b00101732ce",
"message": "Berlin baby",
"hearts": 37,
"createdAt": "2025-05-19T22:07:08.999Z",
"createdAt": "2025-05-31T02:07:08.999Z",
"__v": 0
},
{
Expand Down Expand Up @@ -112,10 +112,10 @@
"__v": 0
},
{
"_id": "682bab8c12155b00101732ce",
"_id": "682bab8c12155b00101732cc",
"message": "Berlin baby",
"hearts": 37,
"createdAt": "2025-05-19T22:07:08.999Z",
"createdAt": "2025-05-30T02:07:08.999Z",
"__v": 0
}
]
11 changes: 11 additions & 0 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { User } from "../models/user"

export const authenticateUser = async (req, res, next) => {
const user = await User.findOne({accessToken: req.header("Authorization")})
if(user) {
req.user = user
next()
} else {
res.status(401).json({loggedOut: true})
}
}
25 changes: 25 additions & 0 deletions models/thought.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superclear structure on the models and its easy to follow your code

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from "mongoose";

const thoughtSchema = new mongoose.Schema({
message: {
type: String,
required: true,
minlength: 5,
maxlength: 140
},
hearts: {
type: Number,
default: 0
},
createdAt: {
type: Date,
default: Date.now
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
}
Comment on lines +18 to +22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})

export const Thought = mongoose.model("Thought", thoughtSchema)
30 changes: 30 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//import bcrypt from "bcrypt"
import crypto from "crypto"
import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
minlength: 3,
maxlength: 100
},
password: {
type: String,
required: true,
minlength: 3,
maxlength: 100
},
accessToken: {
type: String,
default: () => crypto.randomBytes(128).toString("hex")
},
createdAt: {
type: Date,
default: Date.now
}
})

export const User = mongoose.model("User", userSchema)

67 changes: 67 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
W1:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear todo list! Love it.

[x] Your API should have at least three (for now) routes. Try to push yourself to do more, though!
[x] The endpoint "/" should return documentation of your API using e.g. Express List Endpoints
[x] A minimum of one endpoint to return a collection of results (an array of elements).
[x] A minimum of one endpoint to return a single result (single element).
[x] Your API should be RESTful
[x] You should follow the guidelines on how to write clean code.

W2:
[X] Your API should use Mongoose models to model your data and use these models to fetch data from the database.
[x] Your API should validate user input and return appropriate errors if the input is invalid.
[x] You should implement error handling for all your routes, with proper response statuses.
[x] Your frontend should be updated with the possibility to Update and Delete a thought.
[x] Update
[x] Delete
[x] Deployed backend API with deployed DB
[x] Posibility to:
[x] POST
[x] PATCH
[x] DELETE
[x] Frontend posibility to POST (to your API)

W3:
[x] Signup and Login existing Happy Thoughts
[x] Your API should have routes to register and log in
[x] Your endpoints to Create, Update and Delete should be authenticated
[x] Your frontend should have a registration form which POSTs to the API to create a new user.
[x] Your frontend should have a login form to authenticate the user.
[x] Your passwords in the database should be encrypted with bcrypt.
[x] You should implement error handling. Your API should let the user know if something went wrong. Be as specific as possible to help the user, e.g. by validating the user input when creating a new user, and return "That email address already exists". Include correct status codes. Error messages should also be shown on the frontend.
[x] The validation should ensure unique email addresses and/or usernames, depending on how you'd like to structure your User model.


Requirements:
[x] Your API must have at least the following routes. Try to push yourself to do more, though! Endpoints for:
[x] Documentation of your API using e.g. Express List Endpoints
[x] Reading thoughts
[x] Reading a single thought
[x] Liking a thought
[x] Creating a thought (authenticated)
[x] Updating a thought (authenticated)
[x] Deleting a thought (authenticated)
[x] Signing up
[x] Logging in
[x] Your API should be RESTful
[x] You should follow the guidelines on how to write clean code
[x] Your API should use Mongoose models to model your data and use these models to fetch data from the database.
[x] Your API should validate user input and return appropriate errors if the input is invalid.
[x] The validation should ensure unique email addresses and/or usernames depending on how you'd like to structure your User model.
[x] You should implement error handling for all your routes, with proper response statuses.
[x] Your frontend should be updated with the possibility to Update and Delete a thought, as well as signing up and logging in and some error handling if something goes wrong.
[x] Your passwords in the database should be encrypted with bcrypt.
[x] Your API should be deployed to Render or similar.
[x] Everything in your backend should be reflected in your frontend.

Stretch goal options:
[] Allow anonymous (not-logged-in) users to post thoughts
[] As a logged-in user, you should be able to see a list of the thoughts that you've liked.
[] Give the thoughts a category or tags. So you could organise them. For example, 'Food thoughts', 'Project thoughts', 'Home thoughts', etc.
[] Add filtering and sorting options to the endpoint which returns all thoughts. Examples:
[] Sorting on date or number of likes
[] Filtering to only see thoughts with more than x hearts, thoughts newer than x date or thoughts in a specific category (if you implemented categories)
[] Implement pagination in your backend & frontend so you can click through pages of thoughts. The frontend could request a specific page and show only that page. The backend would take the request for that page and return only the thoughts for that page.
[] You could also experiment with implementing infinite scrolling on the frontend rather than having a list of page numbers. This idea is similar to paging and involves frontend & backend changes.
[] When registering, display error messages from the API in the frontend, next to the field which has the error. For example, if the email address is invalid, show an error message next to the email input.
[] In the frontend, store the token in localStorage and send it in headers to persist the logged-in state for the user.
[] Validate the email address format using a regular expression.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^4.17.3",
"express-list-endpoints": "^7.1.1",
"mongoose": "^8.15.1",
"nodemon": "^3.0.1"
}
}
Loading