From 663a98507ee5721561d9612928d8d6f42f474253 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 11:57:53 +0200 Subject: [PATCH 01/27] add npm dependencies: mongoDB och express.list-endpoints --- package.json | 4 +++- server.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1c371b45..afc5c4e9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", + "express-list-endpoints": "^7.1.0", + "mongodb": "^6.6.2", "mongoose": "^8.0.0", "nodemon": "^3.0.1" } -} \ No newline at end of file +} diff --git a/server.js b/server.js index dfe86fb8..48cb4182 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; +import expressListEndpoints from "express-list-endpoints"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; mongoose.connect(mongoUrl); From a7e5e61ce4b483cad6ec693073180d2e9230970c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 13:07:03 +0200 Subject: [PATCH 02/27] add mongoose moddleware handling --- server.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 48cb4182..007ebf12 100644 --- a/server.js +++ b/server.js @@ -3,25 +3,53 @@ import express from "express"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo"; +const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; 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 +// PORT=8080 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()); +app.use((req, res, next) => { + if (mongoose.connection.readyState === 1) { + next() + } else { + res.status(503).json({ error: "Service unavailable." }) + } +}) + +//Thoughts model +const Thought = mongoose.model("Thought", { + message: { + type: String, + required: true, + minlength: 5, + maxlength: 140 + }, + hearts: { + type: Number, + default: 0 + }, + createdAt: { + type: Date, + default: () => new Date() + } +}) // Start defining your routes here app.get("/", (req, res) => { res.send("Hello Technigo!"); }); +app.post("/thoughts", async (req, res) => { + console.log("hello") +}) + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); From 76a6ca5806423c93907353494ab572e7deb7af52 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 13:08:21 +0200 Subject: [PATCH 03/27] add Thought model --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 007ebf12..8f78b0d9 100644 --- a/server.js +++ b/server.js @@ -23,7 +23,7 @@ app.use((req, res, next) => { } }) -//Thoughts model +//Thought model const Thought = mongoose.model("Thought", { message: { type: String, From 32d904878cae11eda0126a8f981e14e79c39d46f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 13:31:03 +0200 Subject: [PATCH 04/27] save middleare and schema to a separate js file --- middleware.js | 19 +++++++++++++++++++ schema.js | 20 ++++++++++++++++++++ server.js | 42 +++++++++++++++--------------------------- 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 middleware.js create mode 100644 schema.js diff --git a/middleware.js b/middleware.js new file mode 100644 index 00000000..c829f21a --- /dev/null +++ b/middleware.js @@ -0,0 +1,19 @@ +// middleware.js +import cors from "cors"; +import express from "express"; +import mongoose from "mongoose"; + +// Middleware to enable CORS +export const corsMiddleware = cors(); + +// Middleware to parse JSON bodies +export const jsonMiddleware = express.json(); + +// Middleware to check MongoDB connection status +export const mongoConnectionMiddleware = (req, res, next) => { + if (mongoose.connection.readyState === 1) { + next(); + } else { + res.status(503).json({ error: "Service unavailable." }); + } +}; diff --git a/schema.js b/schema.js new file mode 100644 index 00000000..ec6e953d --- /dev/null +++ b/schema.js @@ -0,0 +1,20 @@ +import mongoose from 'mongoose'; + +const { Schema } = mongoose; + +export const thougthSchema = new Schema ({ + message: { + type: String, + required: true, + minlength: 5, + maxlength: 140 + }, + hearts: { + type: Number, + default: 0 + }, + createdAt: { + type: Date, + default: () => new Date() + } +}) \ No newline at end of file diff --git a/server.js b/server.js index 8f78b0d9..e5e21457 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,8 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; +import { thougthSchema } from "./schema"; +import { corsMiddleware, jsonMiddleware, mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl); @@ -13,33 +15,12 @@ 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()); -app.use((req, res, next) => { - if (mongoose.connection.readyState === 1) { - next() - } else { - res.status(503).json({ error: "Service unavailable." }) - } -}) +app.use(corsMiddleware); +app.use(jsonMiddleware); +app.use(mongoConnectionMiddleware); //Thought model -const Thought = mongoose.model("Thought", { - message: { - type: String, - required: true, - minlength: 5, - maxlength: 140 - }, - hearts: { - type: Number, - default: 0 - }, - createdAt: { - type: Date, - default: () => new Date() - } -}) +const Thought = mongoose.model("Thought", thougthSchema) // Start defining your routes here app.get("/", (req, res) => { @@ -47,8 +28,15 @@ app.get("/", (req, res) => { }); app.post("/thoughts", async (req, res) => { - console.log("hello") -}) + try { + const { message } = req.body; + const thought = new Thought({ message }); + const savedThought = await thought.save(); + res.status(201).json(savedThought); + } catch (error) { + res.status(400).json({ error: error.message }); + } +}); // Start the server app.listen(port, () => { From cfe75a3d5d7163b9086b4050ce06a41789ae9a8c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 15:13:25 +0200 Subject: [PATCH 05/27] app.post '/thougts' create and test in postman, pause --- server.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index e5e21457..31df37a2 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,7 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; -import { thougthSchema } from "./schema"; +//import { thougthSchema } from "./schema"; import { corsMiddleware, jsonMiddleware, mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; @@ -19,12 +19,37 @@ app.use(corsMiddleware); app.use(jsonMiddleware); app.use(mongoConnectionMiddleware); +//Thought Schema +const { Schema } = mongoose; + +const thoughtSchema = new Schema({ + message: { + type: String, + required: true, + minlength: 5, + maxlength: 140, + }, + hearts: { + type: Number, + default: 0, + }, + createdAt: { + type: Date, + default: () => new Date(), + }, +}); + //Thought model -const Thought = mongoose.model("Thought", thougthSchema) +const Thought = mongoose.model("Thought", thoughtSchema); // Start defining your routes here app.get("/", (req, res) => { - res.send("Hello Technigo!"); + const endpoints = expressListEndpoints(app); + const documentation = { + Welcome: "This is the Happy Thoughts API!", + Endpoints: endpoints, + }; + res.json(documentation); }); app.post("/thoughts", async (req, res) => { From 73f010dd5eb4ca42d88eae7bc8efcfaeec65bad8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 18:50:56 +0200 Subject: [PATCH 06/27] seems that every in the API is working, save and deploy --- middleware.js | 8 -------- server.js | 35 ++++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/middleware.js b/middleware.js index c829f21a..7b47a629 100644 --- a/middleware.js +++ b/middleware.js @@ -1,14 +1,6 @@ // middleware.js -import cors from "cors"; -import express from "express"; import mongoose from "mongoose"; -// Middleware to enable CORS -export const corsMiddleware = cors(); - -// Middleware to parse JSON bodies -export const jsonMiddleware = express.json(); - // Middleware to check MongoDB connection status export const mongoConnectionMiddleware = (req, res, next) => { if (mongoose.connection.readyState === 1) { diff --git a/server.js b/server.js index 31df37a2..afd6238f 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ import express from "express"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; //import { thougthSchema } from "./schema"; -import { corsMiddleware, jsonMiddleware, mongoConnectionMiddleware } from "./middleware"; +import { mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl); @@ -15,8 +15,8 @@ const port = process.env.PORT || 8080; const app = express(); // Add middlewares to enable cors and json body parsing -app.use(corsMiddleware); -app.use(jsonMiddleware); +app.use(cors()); +app.use(express.json()); app.use(mongoConnectionMiddleware); //Thought Schema @@ -42,7 +42,7 @@ const thoughtSchema = new Schema({ //Thought model const Thought = mongoose.model("Thought", thoughtSchema); -// Start defining your routes here +//Here the routes GET and POST app.get("/", (req, res) => { const endpoints = expressListEndpoints(app); const documentation = { @@ -52,17 +52,34 @@ app.get("/", (req, res) => { res.json(documentation); }); -app.post("/thoughts", async (req, res) => { +app.get("/thoughts", async (req, res) => { try { - const { message } = req.body; - const thought = new Thought({ message }); - const savedThought = await thought.save(); - res.status(201).json(savedThought); + const thoughts = await Thought.find().sort({ createdAt: -1 }).limit(20); + res.status(200).json(thoughts); } catch (error) { res.status(400).json({ error: error.message }); } }); +app.post("/thoughts/:thoughtId/like", async (req, res) => { + try { + const { thoughtId } = req.params; + const thought = await Thought.findByIdAndUpdate( + thoughtId, + { $inc: { hearts: 1 } }, + { new: true } + ); + + if (!thought) { + return res.status(404).json({ error: "Thought not found" }); + } + + res.status(200).json(thought); + } catch (error) { + res.status(400).json({ error: error.message }); + } +}) + // Start the server app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); From 4783c0ded15f62ab42eb907f67de76e376976a19 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 19:09:39 +0200 Subject: [PATCH 07/27] deploy to render and changed README --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6a75d8e1..d352e769 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,11 @@ # 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 week's project was about creating the backend for the Happy Thoughts project we did earlier in the bootcamp. This time, I had to code the server of the API where data from the MongoDB database can be handled using GET and POST methods. ## 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? +The main challenge was understanding and implementing the necessary code to connect to a database within a short timeframe. ## 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. +[here](https://project-happy-thoughts-api-d6ds.onrender.com) From bde6de330b0157dbbeab30951b3d9694a8447d80 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 19:15:33 +0200 Subject: [PATCH 08/27] deploy failed to render, changed schema file --- schema.js | 2 +- server.js | 22 +--------------------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/schema.js b/schema.js index ec6e953d..ba0da6ec 100644 --- a/schema.js +++ b/schema.js @@ -2,7 +2,7 @@ import mongoose from 'mongoose'; const { Schema } = mongoose; -export const thougthSchema = new Schema ({ +export const thoughtSchema = new Schema ({ message: { type: String, required: true, diff --git a/server.js b/server.js index afd6238f..4d5812c4 100644 --- a/server.js +++ b/server.js @@ -2,7 +2,7 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; -//import { thougthSchema } from "./schema"; +import { thoughtSchema } from "./schema"; import { mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; @@ -19,26 +19,6 @@ app.use(cors()); app.use(express.json()); app.use(mongoConnectionMiddleware); -//Thought Schema -const { Schema } = mongoose; - -const thoughtSchema = new Schema({ - message: { - type: String, - required: true, - minlength: 5, - maxlength: 140, - }, - hearts: { - type: Number, - default: 0, - }, - createdAt: { - type: Date, - default: () => new Date(), - }, -}); - //Thought model const Thought = mongoose.model("Thought", thoughtSchema); From 5677b05f295ae434d64e1d5f1041a543cd640cef Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 20:00:30 +0200 Subject: [PATCH 09/27] deploy failed to render, add changes in mongoose.connect --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 4d5812c4..2ab319c2 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,7 @@ import { thoughtSchema } from "./schema"; import { mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; -mongoose.connect(mongoUrl); +mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });//I have some problems deploying I keep the second parameter even if this is old coding ;) mongoose.Promise = Promise; // Defines the port the app will run on. Defaults to 8080, but can be overridden From e8612aef910a7c0e5a16a45e4e1f93c2329151a7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 21:24:59 +0200 Subject: [PATCH 10/27] add changes to the code, kill schema.js and mideware.js KISS metodology --- middleware.js | 11 ----------- schema.js | 20 -------------------- server.js | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 37 deletions(-) delete mode 100644 middleware.js delete mode 100644 schema.js diff --git a/middleware.js b/middleware.js deleted file mode 100644 index 7b47a629..00000000 --- a/middleware.js +++ /dev/null @@ -1,11 +0,0 @@ -// middleware.js -import mongoose from "mongoose"; - -// Middleware to check MongoDB connection status -export const mongoConnectionMiddleware = (req, res, next) => { - if (mongoose.connection.readyState === 1) { - next(); - } else { - res.status(503).json({ error: "Service unavailable." }); - } -}; diff --git a/schema.js b/schema.js deleted file mode 100644 index ba0da6ec..00000000 --- a/schema.js +++ /dev/null @@ -1,20 +0,0 @@ -import mongoose from 'mongoose'; - -const { Schema } = mongoose; - -export const thoughtSchema = new Schema ({ - message: { - type: String, - required: true, - minlength: 5, - maxlength: 140 - }, - hearts: { - type: Number, - default: 0 - }, - createdAt: { - type: Date, - default: () => new Date() - } -}) \ No newline at end of file diff --git a/server.js b/server.js index 2ab319c2..93dee29c 100644 --- a/server.js +++ b/server.js @@ -1,9 +1,8 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; +//import Thought from "./model.js" import expressListEndpoints from "express-list-endpoints"; -import { thoughtSchema } from "./schema"; -import { mongoConnectionMiddleware } from "./middleware"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });//I have some problems deploying I keep the second parameter even if this is old coding ;) @@ -17,17 +16,42 @@ const app = express(); // Add middlewares to enable cors and json body parsing app.use(cors()); app.use(express.json()); -app.use(mongoConnectionMiddleware); +app.use((req, res, next) => { + if (mongoose.connection.readyState === 1) { + next(); + } else { + res.status(503).json({ error: "Service unavailable." }); + } +}); -//Thought model -const Thought = mongoose.model("Thought", thoughtSchema); +//Models +const Thought = mongoose.model("Thought", { + message: { + type: String, + required: true, + minlength: 5, + maxlength: 140 + }, + hearts: { + type: Number, + default: 0 + }, + createdAt: { + type: Date, + default: () => new Date() + } +}) //Here the routes GET and POST app.get("/", (req, res) => { const endpoints = expressListEndpoints(app); const documentation = { Welcome: "This is the Happy Thoughts API!", - Endpoints: endpoints, + Endpoints: { + "/": "Get API documentation", + "/thoughts": "Get 20 latest thoughts", + "/thoughts/:thoughtId/like": "Like a specific thought" + } }; res.json(documentation); }); From c3a3aafeb14478b6878ff1e0e4352cdc5e376573 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 21:29:45 +0200 Subject: [PATCH 11/27] add changes to the code, remove MONGO DRIVER from line 8 --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 93dee29c..0bd5991e 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,7 @@ import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; -mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });//I have some problems deploying I keep the second parameter even if this is old coding ;) +mongoose.connect(mongoUrl); mongoose.Promise = Promise; // Defines the port the app will run on. Defaults to 8080, but can be overridden From a6b2b66b93b45f053180ccfc01f8a2bcb63661ba Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 21:31:12 +0200 Subject: [PATCH 12/27] remove pull template --- pull_request_template.md | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 pull_request_template.md diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index d92c89b5..00000000 --- a/pull_request_template.md +++ /dev/null @@ -1,7 +0,0 @@ -## Netlify link -Add your Netlify link here. -PS. Don't forget to add it in your readme as well. - -## Collaborators -Add your collaborators here. Write their GitHub usernames in square brackets. If there's more than one, separate them with a comma, like this: -[github-username-1, github-username-2] From 0853e7f62ffd7f077b08e3f58749e6ff749087d8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 21:47:04 +0200 Subject: [PATCH 13/27] add devDependencies in package.jonson, for bavel/core and preset-env --- package.json | 6 ++++-- server.js | 1 - 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index afc5c4e9..562b5dc6 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,6 @@ "author": "", "license": "ISC", "dependencies": { - "@babel/core": "^7.17.9", - "@babel/node": "^7.16.8", "@babel/preset-env": "^7.16.11", "cors": "^2.8.5", "express": "^4.17.3", @@ -18,5 +16,9 @@ "mongodb": "^6.6.2", "mongoose": "^8.0.0", "nodemon": "^3.0.1" + }, + "devDependencies": { + "@babel/core": "^7.17.9", + "@babel/preset-env": "^7.16.11" } } diff --git a/server.js b/server.js index 0bd5991e..8374ce83 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,6 @@ import cors from "cors"; import express from "express"; import mongoose from "mongoose"; -//import Thought from "./model.js" import expressListEndpoints from "express-list-endpoints"; const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; From 53bfe1f9b4010c7c71e8aead9880269b39f846c8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 21:48:16 +0200 Subject: [PATCH 14/27] run npm install after making last changes on package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 562b5dc6..66b73c36 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "author": "", "license": "ISC", "dependencies": { - "@babel/preset-env": "^7.16.11", + "@babel/node": "^7.16.8", "cors": "^2.8.5", "express": "^4.17.3", "express-list-endpoints": "^7.1.0", From ab21dc52561ddd1102ff0b36321e73c23bba6583 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 18 May 2024 22:48:00 +0200 Subject: [PATCH 15/27] this will not save the deploy problem but now the app.post/thougths is back --- server.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server.js b/server.js index 8374ce83..eab20fdc 100644 --- a/server.js +++ b/server.js @@ -64,6 +64,23 @@ app.get("/thoughts", async (req, res) => { } }); +app.post("/thoughts", async (req, res) => { + try { + const thought = new Thought({message: req.body.message}) + await thought.save() + res.status(201).json({ + success: true, + response: thought, + message: "Thought posted", + }) + } catch (error) { + res.status(400).json({ + success: false, + response: error, + message: "Could not save thought."}) + } +}) + app.post("/thoughts/:thoughtId/like", async (req, res) => { try { const { thoughtId } = req.params; From 2fa0a41e4c988c2c2cb8fe1a5161b53808259945 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 May 2024 01:08:28 +0200 Subject: [PATCH 16/27] add .env file open Atlas account and create Arnau-cluster, create secret url --- package.json | 1 + server.js | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/package.json b/package.json index 66b73c36..94383a3c 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "@babel/node": "^7.16.8", "cors": "^2.8.5", + "dotenv": "^16.4.5", "express": "^4.17.3", "express-list-endpoints": "^7.1.0", "mongodb": "^6.6.2", diff --git a/server.js b/server.js index eab20fdc..b8b8f6c5 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,12 @@ import cors from "cors"; import express from "express"; +import dotenv from "dotenv" import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; +// Load environment variables +dotenv.config({ path: ".env" }); + const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; From 26d4e0cc77125779f049435f2b39878bc71890e2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 May 2024 10:38:19 +0200 Subject: [PATCH 17/27] correct MONGO_URL on .env --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index b8b8f6c5..eacea115 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,7 @@ import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; // Load environment variables -dotenv.config({ path: ".env" }); +dotenv.config(); const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl); From 4fc2ad2e3e96c67de87c46c846f7bafb6b621b47 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 May 2024 15:36:08 +0200 Subject: [PATCH 18/27] add forntend file on README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d352e769..36d79c13 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,5 @@ The main challenge was understanding and implementing the necessary code to conn ## View it live -[here](https://project-happy-thoughts-api-d6ds.onrender.com) +Backend: [here](https://project-happy-thoughts-api-d6ds.onrender.com) +Frontend: [here](https://my-happy-thoughts-project.netlify.app/) From ab4a57f8df8e697c13ce1d0b4c47274a9738055c Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 May 2024 16:32:07 +0200 Subject: [PATCH 19/27] push changes and try again --- server.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server.js b/server.js index eacea115..48329e96 100644 --- a/server.js +++ b/server.js @@ -11,7 +11,6 @@ const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; -// Defines the port the app will run on. Defaults to 8080, but can be overridden // PORT=8080 npm start const port = process.env.PORT || 8080; const app = express(); From c67cdae3f94ec401d39ba99e7d186321be61b970 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 19 May 2024 17:02:24 +0200 Subject: [PATCH 20/27] STUCK, server not connecting to MongoDB compass --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36d79c13..9902aa09 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This week's project was about creating the backend for the Happy Thoughts projec ## The problem -The main challenge was understanding and implementing the necessary code to connect to a database within a short timeframe. +The main challenge was understanding and implementing the necessary code to connect to a database within a short timeframe. I got stuck while trying to connect to server to MongoDB compass no matter what it wasn't connecting. ## View it live From 528f7509517c547c9253bec6349c980b5c3f6e04 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 00:03:54 +0200 Subject: [PATCH 21/27] exiting, after hours without thoughts collection in compass now finally apears after changing to mongoUrl path save and run again --- package.json | 2 +- server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 94383a3c..57432cc6 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "express": "^4.17.3", "express-list-endpoints": "^7.1.0", "mongodb": "^6.6.2", - "mongoose": "^8.0.0", + "mongoose": "^8.4.0", "nodemon": "^3.0.1" }, "devDependencies": { diff --git a/server.js b/server.js index 48329e96..94044b00 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,7 @@ import expressListEndpoints from "express-list-endpoints"; // Load environment variables dotenv.config(); -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happy-thoughts"; +const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"; mongoose.connect(mongoUrl); mongoose.Promise = Promise; From 6d430365c569e64ac7e7ff03d8aec33a6cee1f58 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 00:16:25 +0200 Subject: [PATCH 22/27] change render url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9902aa09..3221e61e 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,5 @@ The main challenge was understanding and implementing the necessary code to conn ## View it live -Backend: [here](https://project-happy-thoughts-api-d6ds.onrender.com) +Backend: [here](https://project-happy-thoughts-api-w8r4.onrender.com) Frontend: [here](https://my-happy-thoughts-project.netlify.app/) From 49ffa8963c31515d1293354f2b872946523760bd Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 00:32:12 +0200 Subject: [PATCH 23/27] create a new cluster in atlas and add a new .env url string --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 94044b00..714c4a2f 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,7 @@ import expressListEndpoints from "express-list-endpoints"; // Load environment variables dotenv.config(); -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"; +const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts" mongoose.connect(mongoUrl); mongoose.Promise = Promise; From 4dccb98370393ef95d68e40fcbdfd998529b0f8b Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 00:53:52 +0200 Subject: [PATCH 24/27] uppdate connection code enl GPT --- server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 714c4a2f..304ff7fc 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,9 @@ import expressListEndpoints from "express-list-endpoints"; dotenv.config(); const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts" -mongoose.connect(mongoUrl); +mongoose.connect(mongoUrl,{ useNewUrlParser: true, useUnifiedTopology: true }) +.then(() => console.log("MongoDB connected")) +.catch(err => console.error("MongoDB connection error:", err)); mongoose.Promise = Promise; // PORT=8080 npm start From 694892efa31e4ce3997c73856b3dad5098247eff Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 09:50:22 +0200 Subject: [PATCH 25/27] uppdate MONGO_URL variable --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 304ff7fc..55ab7f1f 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,7 @@ import expressListEndpoints from "express-list-endpoints"; dotenv.config(); const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts" -mongoose.connect(mongoUrl,{ useNewUrlParser: true, useUnifiedTopology: true }) +mongoose.connect(mongoUrl) .then(() => console.log("MongoDB connected")) .catch(err => console.error("MongoDB connection error:", err)); mongoose.Promise = Promise; From 328e28e05980979de0c06658c3a86088b28f4f19 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 11:15:15 +0200 Subject: [PATCH 26/27] uppdated password on MONGO_URL and collection name in url --- server.js | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/server.js b/server.js index 55ab7f1f..39e63e5d 100644 --- a/server.js +++ b/server.js @@ -1,16 +1,17 @@ import cors from "cors"; import express from "express"; -import dotenv from "dotenv" +import dotenv from "dotenv"; import mongoose from "mongoose"; import expressListEndpoints from "express-list-endpoints"; // Load environment variables dotenv.config(); -const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts" -mongoose.connect(mongoUrl) -.then(() => console.log("MongoDB connected")) -.catch(err => console.error("MongoDB connection error:", err)); +const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"; +mongoose + .connect(mongoUrl) + .then(() => console.log("MongoDB connected")) + .catch((err) => console.error("MongoDB connection error:", err)); mongoose.Promise = Promise; // PORT=8080 npm start @@ -34,17 +35,17 @@ const Thought = mongoose.model("Thought", { type: String, required: true, minlength: 5, - maxlength: 140 + maxlength: 140, }, hearts: { type: Number, - default: 0 + default: 0, }, createdAt: { - type: Date, - default: () => new Date() - } -}) + type: Date, + default: () => new Date(), + }, +}); //Here the routes GET and POST app.get("/", (req, res) => { @@ -54,8 +55,8 @@ app.get("/", (req, res) => { Endpoints: { "/": "Get API documentation", "/thoughts": "Get 20 latest thoughts", - "/thoughts/:thoughtId/like": "Like a specific thought" - } + "/thoughts/:thoughtId/like": "Like a specific thought", + }, }; res.json(documentation); }); @@ -71,20 +72,21 @@ app.get("/thoughts", async (req, res) => { app.post("/thoughts", async (req, res) => { try { - const thought = new Thought({message: req.body.message}) - await thought.save() + const thought = new Thought({ message: req.body.message }); + await thought.save(); res.status(201).json({ - success: true, - response: thought, - message: "Thought posted", - }) + success: true, + response: thought, + message: "Thought posted", + }); } catch (error) { - res.status(400).json({ + res.status(400).json({ success: false, response: error, - message: "Could not save thought."}) + message: "Could not save thought.", + }); } -}) +}); app.post("/thoughts/:thoughtId/like", async (req, res) => { try { @@ -103,7 +105,7 @@ app.post("/thoughts/:thoughtId/like", async (req, res) => { } catch (error) { res.status(400).json({ error: error.message }); } -}) +}); // Start the server app.listen(port, () => { From a27b300a90f69b7fc841c80a9beecfa0b5ca3d11 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 May 2024 11:50:06 +0200 Subject: [PATCH 27/27] removed some extra code on mongoose.connect --- server.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/server.js b/server.js index 39e63e5d..9fc564df 100644 --- a/server.js +++ b/server.js @@ -8,10 +8,7 @@ import expressListEndpoints from "express-list-endpoints"; dotenv.config(); const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/happyThoughts"; -mongoose - .connect(mongoUrl) - .then(() => console.log("MongoDB connected")) - .catch((err) => console.error("MongoDB connection error:", err)); +mongoose.connect(mongoUrl); mongoose.Promise = Promise; // PORT=8080 npm start