-
Notifications
You must be signed in to change notification settings - Fork 517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week 19: happy thoughts API #509
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
# Project Happy Thoughts API | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
This project is a simple REST API for storing, retrieving, and liking “Happy Thoughts”. Inspired by Twitter, but focusing on positivity. It includes endpoints to get recent thoughts, post new ones, and “like” them. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
I wanted to build an API that could integrate easily with a React frontend for a “happy thoughts” feed. The main goals were: | ||
|
||
To practice designing a MongoDB-backed API with Mongoose models. | ||
To handle data validation (e.g., message length limits). | ||
To deploy the API so that it can be used in a production-like setting. | ||
Tools & Techniques | ||
Node.js / Express for the server. | ||
Mongoose for modeling and validation. | ||
MongoDB for storage (hosted on [Mongo Atlas / local / etc.]). | ||
Heroku / Render / other for deployment. | ||
dotenv for environment variables. | ||
If I had more time, I would add authentication, user profiles, and more thorough error handling. I’d also consider pagination or infinite scroll to handle large numbers of thoughts. | ||
|
||
Endpoints | ||
GET /thoughts | ||
Returns the 20 most recent thoughts, sorted by creation date. | ||
POST /thoughts | ||
Creates a new thought given a valid message. | ||
POST /thoughts/:id/like | ||
Increments or toggles the like count of a specific thought by ID. | ||
|
||
## 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. | ||
[Deployed API link](https://project-happy-thoughts-api-hc1b.onrender.com/) Click to see the available endpoints and try them out! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,141 @@ | ||
import cors from "cors"; | ||
import express from "express"; | ||
import mongoose from "mongoose"; | ||
|
||
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; | ||
mongoose.connect(mongoUrl); | ||
mongoose.Promise = Promise; | ||
|
||
// Defines the port the app will run on. Defaults to 8080, but can be overridden | ||
// when starting the server. Example command to overwrite PORT env variable value: | ||
// PORT=9000 npm start | ||
const port = process.env.PORT || 8080; | ||
const app = express(); | ||
|
||
// Add middlewares to enable cors and json body parsing | ||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
// Start defining your routes here | ||
app.get("/", (req, res) => { | ||
res.send("Hello Technigo!"); | ||
}); | ||
|
||
// Start the server | ||
import express from 'express' | ||
import cors from 'cors' | ||
import mongoose from 'mongoose' | ||
import dotenv from 'dotenv' | ||
import listEndpoints from 'express-list-endpoints' | ||
|
||
dotenv.config() | ||
|
||
// ---------------------- | ||
// 1. Mongoose Connection | ||
// ---------------------- | ||
const MONGO_URL = process.env.MONGODB_URI || 'mongodb://127.0.0.1/happy-thoughts' | ||
mongoose.connect(MONGO_URL, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
.then(() => { | ||
console.log('Connected to MongoDB successfully!') | ||
}) | ||
.catch((err) => { | ||
console.error('Failed to connect to MongoDB:', err) | ||
}) | ||
|
||
// ---------------------- | ||
// 2. Mongoose Model | ||
// ---------------------- | ||
const ThoughtSchema = new mongoose.Schema({ | ||
message: { | ||
type: String, | ||
required: true, | ||
minlength: 5, | ||
maxlength: 140 | ||
}, | ||
hearts: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
// Example array of user references who have “liked” this thought | ||
likedUsers: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User' | ||
} | ||
], | ||
createdAt: { | ||
type: Date, | ||
default: () => new Date() | ||
} | ||
}) | ||
|
||
const Thought = mongoose.model('Thought', ThoughtSchema) | ||
|
||
// ---------------------- | ||
// 3. Express App Setup | ||
// ---------------------- | ||
const app = express() | ||
const port = process.env.PORT || 8080 | ||
|
||
// Middlewares | ||
app.use(cors({ | ||
origin: 'https://happiestthoughts.netlify.app', | ||
methods: ['GET', 'POST'], | ||
allowedHeaders: ['Content-Type', 'Authorization'] | ||
})) | ||
app.use(express.json()) | ||
|
||
// ---------------------- | ||
// 4. Endpoints | ||
// ---------------------- | ||
|
||
// Root endpoint - shows available endpoints in JSON | ||
app.get('/', (req, res) => { | ||
console.log('REQ.BODY =>', req.body); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No console.logs in production |
||
res.json({ | ||
message: 'Welcome to the Happy Thoughts API!', | ||
endpoints: listEndpoints(app) | ||
}) | ||
}) | ||
|
||
// GET /thoughts - Return up to 20 thoughts, sorted by date (descending) | ||
app.get('/thoughts', async (req, res) => { | ||
try { | ||
const thoughts = await Thought.find() | ||
.sort({ createdAt: 'desc' }) | ||
.limit(20) | ||
.exec() | ||
res.status(200).json(thoughts) | ||
} catch (err) { | ||
console.error('Error fetching thoughts:', err) | ||
res.status(500).json({ error: 'Could not fetch thoughts' }) | ||
} | ||
}) | ||
|
||
// POST /thoughts - Create a new thought | ||
app.post('/thoughts', async (req, res) => { | ||
const { message } = req.body | ||
|
||
try { | ||
const newThought = new Thought({ message }) | ||
const savedThought = await newThought.save() | ||
res.status(201).json(savedThought) | ||
} catch (err) { | ||
console.error('Error creating new thought:', err) | ||
res.status(400).json({ | ||
error: 'Could not save thought', | ||
details: err.errors | ||
}) | ||
} | ||
}) | ||
|
||
// POST /thoughts/:id/like - Toggle hearts for a given thought | ||
app.post('/thoughts/:id/like', async (req, res) => { | ||
try { | ||
const { id } = req.params | ||
const thought = await Thought.findById(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use findByIdAndUpdate for updating hearts directly instead of fetching, modifying, and saving? 🤔 |
||
|
||
if (!thought) { | ||
return res.status(404).json({ error: 'Thought not found' }) | ||
} | ||
|
||
// Example toggle logic: if hearts is even, increment, otherwise decrement | ||
if (thought.hearts % 2 === 0) { | ||
thought.hearts += 1 | ||
} else { | ||
thought.hearts -= 1 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this? 👀 |
||
|
||
const updatedThought = await thought.save() | ||
res.status(200).json(updatedThought) | ||
} catch (err) { | ||
console.error('Error liking the thought:', err) | ||
res.status(500).json({ error: 'Could not update hearts' }) | ||
} | ||
}) | ||
|
||
// ---------------------- | ||
// 5. Start the Server | ||
// ---------------------- | ||
app.listen(port, () => { | ||
console.log(`Server running on http://localhost:${port}`); | ||
}); | ||
console.log(`Server running on http://localhost:${port}`) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍