Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d00cdde
Add GET endpoints for thoughts and API documentation
JeffieJansson Jan 20, 2026
a0175a7
add filter of hearts to the endpoint which returns all thoughts
JeffieJansson Jan 21, 2026
8067ba2
installed and imported dotenv
JeffieJansson Jan 26, 2026
5d150e3
import dotenv
JeffieJansson Jan 27, 2026
5ca9828
Import Mongoose, add thought schema, add seeding to db, change old co…
JeffieJansson Jan 28, 2026
4f0e56f
added post endpoint and success message
JeffieJansson Jan 28, 2026
e1749be
add delete,update message and like enpoint with delete, patch and patch
JeffieJansson Jan 29, 2026
4bb1ce5
Switch to MongoDB Atlas for database connection
JeffieJansson Jan 29, 2026
07d6565
debugging loss of POST data when restart of server
JeffieJansson Jan 29, 2026
61bb661
add sorting to make new thought first in thought list
JeffieJansson Jan 30, 2026
bc6187d
update server to use route modules and models and moved routes into s…
JeffieJansson Feb 3, 2026
ea8d0b8
created separate file and added auth middleware
JeffieJansson Feb 3, 2026
2c6c543
changed to routes and moved thoughtroutes to separate file
JeffieJansson Feb 3, 2026
f600bd5
Added signup/login routes
JeffieJansson Feb 3, 2026
2aed55c
created new file and moved thought model to it
JeffieJansson Feb 3, 2026
c00a591
created user model
JeffieJansson Feb 3, 2026
b49b5dd
install bcrypt
JeffieJansson Feb 3, 2026
6b3034f
update code implements that did not get commited from last commit
JeffieJansson Feb 3, 2026
f774ce6
change route from users to user
JeffieJansson Feb 3, 2026
a559ef3
add minlenght to passowrd for signup
JeffieJansson Feb 3, 2026
cd89bc5
trying out password error
JeffieJansson Feb 3, 2026
92ff709
add minimun 6 char to password for signup
JeffieJansson Feb 3, 2026
ab487ba
edit incorrect value and mongoose schema
JeffieJansson Feb 4, 2026
e605f02
removing seed and import of hard coded data and add readme
JeffieJansson Feb 6, 2026
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
74 changes: 68 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,73 @@
# Project API
# Happy Thoughts API

This project includes the packages and babel setup for an express server, and is just meant to make things a little simpler to get up and running with.

## Getting started
## Project Overview

Install dependencies with `npm install`, then start the server by running `npm run dev`
Happy Thoughts API is a RESTful backend built with Node.js, Express, and MongoDB. It provides endpoints for creating, reading, updating, liking, and deleting "thoughts" (messages), as well as user authentication and registration.

## View it live
### Key Features

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.
- **Mongoose Schema Models:** All data is structured and validated using Mongoose models for both thoughts and users. This ensures consistent data and enforces rules such as required fields and minimum password length.
- **Password Security:** User passwords are securely hashed using bcrypt before being stored in the database.
- **Authentication:** The API uses access tokens to protect routes for creating, updating, and deleting thoughts. Only authenticated users can perform these actions.
- **Error Handling:** All routes include robust error handling and return clear status codes and messages for invalid input, authentication failures, and other errors.
- **RESTful Design:** The API follows RESTful principles, with clear separation of resources and HTTP methods for CRUD operations.
- **Filtering & Sorting:** Thoughts can be filtered by number of likes (hearts) and sorted by creation date.
- **Frontend Integration:** Designed to work seamlessly with a frontend application, supporting features like updating and deleting thoughts, user signup/login, and error feedback.

---

## Project Requirements

**Fulfilled requirements:**
- ✅ API documentation with Express List Endpoints
- ✅ Read all thoughts
- ✅ Read a single thought
- ✅ Like a thought
- ✅ Create a thought (authenticated)
- ✅ Update a thought (authenticated)
- ✅ Delete a thought (authenticated)
- ✅ Sign up (register user)
- ✅ Log in (user authentication)
- ✅ RESTful structure
- ✅ Clean code according to guidelines
- ✅ Uses Mongoose models
- ✅ Validates user input
- ✅ Unique email addresses
- ✅ Error handling and status codes
- ✅ Frontend supports Update/Delete/Signup/Login and error handling
- ✅ Passwords encrypted with bcrypt
- ✅ API deployed on Render
- ✅ Backend and frontend are synced
- ✅ Filtering thoughts by number of hearts (`/thoughts?hearts=5`)
- ✅ Sorting by date (newest first)
- ✅ API error messages displayed in frontend during registration
- ✅ Token stored in localStorage and sent in headers

---

## API Endpoints

- `GET /thoughts` – Get all thoughts (with filtering/sorting)
- `GET /thoughts/:id` – Get a single thought
- `POST /thoughts` – Create a thought (requires authentication)
- `PATCH /thoughts/:id/like` – Like a thought
- `PATCH /thoughts/:id` – Update a thought (requires authentication)
- `DELETE /thoughts/:id` – Delete a thought (requires authentication)
- `POST /user/signup` – Register user
- `POST /user/login` – Log in user

---

## File structure
middleware/
└──authMiddleware.js
models/
├── Thought.js
└── User.js
routes/
├── thoughtRoutes.js
└── userRoutes.js

└── server,js
```
23 changes: 23 additions & 0 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import User from "../models/User.js";

const authenticateUser = async (req, res, next) => {
try {
const user = await User.findOne({
accessToken: req.header("Authorization").replace("Bearer ", ""),
});
if (user) {
req.user = user;
next();
} else {
res.status(401).json({
message: "Authentication missing or invalid.",
loggedOut: true,
});
}
} catch (err) {
res
.status(500)
.json({ message: "Internal server error", error: err.message });
}
};
export default authenticateUser;
23 changes: 23 additions & 0 deletions models/Thought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mongoose, { Schema } from "mongoose";

const ThoughtSchema = new Schema({
message: {
type: String,
required: [true, "A Message is required"],
minlength: [5, "A Message must be at least 5 characters"],
maxlength: [140, "A Message cannot exceed 140 characters"],
trim: true,
},
hearts: {
type: Number,
default: 0,
},
createdAt: {
type: Date,
default: Date.now,
},
});

const Thought = mongoose.model("Thought", ThoughtSchema);

export default Thought;
24 changes: 24 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose, { Schema } from "mongoose";
import crypto from "crypto";

const userSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
minlength: 6,
},
accessToken: {
type: String,
required: true,
default: () => crypto.randomBytes(128).toString("hex"),
},
})

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

export default User;
23 changes: 19 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
"name": "project-api",
"version": "1.0.0",
"description": "Project API",
"homepage": "https://github.com/JeffieJansson/ht-project-api#readme",
"bugs": {
"url": "https://github.com/JeffieJansson/ht-project-api/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/JeffieJansson/ht-project-api.git"
},
"license": "ISC",
"author": "",
"type": "commonjs",
"main": "server.js",
"scripts": {
"start": "babel-node server.js",
"dev": "nodemon server.js --exec babel-node"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"express": "^4.17.3",
"nodemon": "^3.0.1"
"dotenv": "^17.2.3",
"express": "^4.22.1",
"express-list-endpoints": "^7.1.1",
"mongodb": "^7.0.0",
"mongoose": "^9.1.5",
"nodemon": "^3.1.11"
}
}
3 changes: 2 additions & 1 deletion pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Please include your Render link here.
Please include your Render link here.
https://ht-api-ij7j.onrender.com/
Loading