Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0d0657e
set up endpoints and created three routes: endpoints documentation, a…
Jan 20, 2026
def493d
made file structure for models,routes and middleware. added routes fo…
Apr 23, 2026
dc6f1fd
added js to authMiddleware for render debugging
Apr 27, 2026
3689eb9
more debugging for render
Apr 27, 2026
eee75ce
changed the happy-thoughts route names
Apr 27, 2026
0f7144b
added updated some of the happy thoughts routes with the new mongoose…
Apr 27, 2026
8ffa85f
added seeding database for happy thoughts testing
Apr 27, 2026
b095cc2
debugged id route: added async await and changed id param
May 20, 2026
788f053
added seeding users to userRoutes
May 20, 2026
5f1cf93
variable change in seeding users
May 20, 2026
5fe76ba
name debug for frontend
May 22, 2026
8a840fa
updated user model with first and last name
May 27, 2026
d85eec1
added first and lastname to signup route and added a get route for a …
May 27, 2026
a41c7e5
updated signup route to send accessToken
May 27, 2026
57f33da
debugging signup route
May 27, 2026
3aed50a
more debugging for signup route
May 27, 2026
d24e384
fixed variable name
May 27, 2026
1f47018
debugg in auth middleware
May 27, 2026
62fc64e
happy thoughts route: added sort by desc to get route
May 27, 2026
93dfe64
added reset users
May 27, 2026
b0adbbe
added email validation to signup and login route and added patch rout…
May 29, 2026
c7bd4ae
added ping route for cron-job, to wake up api
May 29, 2026
64d89eb
added auth middleware to id patch route
May 29, 2026
9fc67b7
commented out seeding thoughts
May 29, 2026
8e5cbd0
deleted seeding datbased
May 29, 2026
b3e90c4
added render link in readme
May 29, 2026
2048618
refactor: enhance authentication and author validation in HappyThough…
Jun 24, 2026
323cea9
refactor: improve author population in HappyThought routes
Jun 24, 2026
a39640d
refactor: enhance author validation in delete route for HappyThoughts
Jun 24, 2026
da1cc94
deleted json data that is no longer in use, updated readme file with …
Aug 18, 2026
9092326
Fix render link formatting in README
carro-barro Aug 18, 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
70 changes: 64 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,69 @@
# Project 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.
# Happy Thoughts API

## Getting started
render link: https://happy-thoughts-api-o47r.onrender.com

Install dependencies with `npm install`, then start the server by running `npm run dev`
This project is a RESTful API built with **Node.js**, **Express**, and **MongoDB** (via Mongoose) that allows users to create, view, manage, and "like" happy thoughts. It also includes user authentication features to secure content management actions.

## 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.
* **User Management:**
* **Signup:** Register new users with email, first name, last name, and hashed passwords (using `bcrypt`).
* **Login:** Authenticate users and receive an `accessToken` for protected requests.
* **Happy Thoughts Management:**
* **Create:** Authenticated users can post happy thoughts.
* **Read:** View all thoughts (supports filtering by minimum likes) or fetch a specific thought by ID.
* **Update:** Authenticated users can edit their own thoughts.
* **Delete:** Authenticated users can delete their own thoughts.
* **Like:** Users can "like" (increment the heart count) any happy thought.
* **Security:**
* Authentication middleware verifies user `accessToken` for sensitive operations (creating, updating, deleting).
* Authorization checks ensure users can only modify thoughts they own.
* Password hashing and email validation.

## 🛠 Tech Stack

* **Runtime:** Node.js
* **Framework:** Express.js
* **Database:** MongoDB
* **ODM:** Mongoose
* **Security:** `bcrypt` (password hashing), `crypto` (access token generation)
* **Utilities:** `cors`, `dotenv` (environment variables), `express-list-endpoints`

## 📡 API Endpoints

### User Routes (`/users`)

| Method | Endpoint | Description | Auth Required |
| :--- | :--- | :--- | :--- |
| `POST` | `/signup` | Register a new user | No |
| `POST` | `/login` | Authenticate user | No |
| `GET` | `/:id` | Get user profile | No |

### Happy Thoughts Routes (`/happy-thoughts`)

| Method | Endpoint | Description | Auth Required |
| :--- | :--- | :--- | :--- |
| `GET` | `/` | List thoughts (query: `minLikes`) | No |
| `POST` | `/` | Create a new thought | Yes |
| `GET` | `/:id` | Get a specific thought | No |
| `PATCH` | `/:id` | Update a thought | Yes |
| `DELETE` | `/:id` | Delete a thought | Yes |
| `PATCH` | `/:id/like` | Like a thought | No |

## ⚙️ Setup & Configuration

1. **Clone the repository.**
2. **Install dependencies:**
```bash
npm install
```
3. **Environment Variables:**
Create a `.env` file in the root directory and define the following:
```text
MONGO_URL=your_mongodb_connection_string
```
4. **Run the server:**
```bash
npm run dev
```
121 changes: 0 additions & 121 deletions data.json

This file was deleted.

33 changes: 33 additions & 0 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { User } from "../models/User.js"

export const authenticateUser = async (request, response, next) => {
try {

const authHeader = request.header("Authorization") || request.get("Authorization")

if(!authHeader) {
return response.status(401).json({
message: "Authentication missing or invalid",
loggedOut: true
})
}

const user = await User.findOne({ accessToken: authHeader.replace("Bearer ", "")})

if (user) {
request.user = user
next()
} else {
response.status(401).json({
message: "Authentication missing or invalid",
loggedOut: true
})
}

} catch (error) {
response.status(500).json({
message: "internal server error",
error: error.message
})
}
}
22 changes: 22 additions & 0 deletions models/HappyThought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose"

const happyThoughtSchema = new mongoose.Schema({
message: {
type: String,
required: true
},
hearts: {
type: Number
},
createdAt: {
type: Date,
default: Date.now
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
}
})

export const HappyThought = mongoose.model("HappyThought", happyThoughtSchema)
28 changes: 28 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import crypto from "crypto"
import mongoose from "mongoose"

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

export const User = mongoose.model("User", userSchema)
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "project-api",
"version": "1.0.0",
"description": "Project API",
"type": "module",
"scripts": {
"start": "babel-node server.js",
"dev": "nodemon server.js --exec babel-node"
Expand All @@ -12,8 +13,13 @@
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"cors": "^2.8.5",
"express": "^4.17.3",
"bcrypt": "^6.0.0",
"cors": "^2.8.6",
"dotenv": "^17.2.3",
"express": "^4.22.1",
"express-list-endpoints": "^7.1.1",
"mongodb": "^7.2.0",
"mongoose": "^9.5.0",
"nodemon": "^3.0.1"
}
}
Loading