From ab3bc6c49afff9fde3f873ea6cb3a5d99f67c938 Mon Sep 17 00:00:00 2001 From: gabrielsanchez Date: Mon, 22 Aug 2022 19:20:21 -0500 Subject: [PATCH 1/3] add model and connection --- src/app.js | 11 ++++++++--- src/users/users.model.js | 8 ++++++++ src/users/users.router.js | 3 +-- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 src/users/users.model.js diff --git a/src/app.js b/src/app.js index 5cfae74..f820895 100644 --- a/src/app.js +++ b/src/app.js @@ -6,9 +6,14 @@ const usersRouter = require("./users/users.router"); const app = express(); app.use(express.json()); -// Write your code here - - +mongoose + .connect(process.env.DATABASE_URL, { + useNewUrlParser: true + }) + .then(() => { + console.log("Connected to MongoDB!") + }) + .catch(error => console.error(error.message)); app.use("/users", usersRouter); diff --git a/src/users/users.model.js b/src/users/users.model.js new file mode 100644 index 0000000..6d85a80 --- /dev/null +++ b/src/users/users.model.js @@ -0,0 +1,8 @@ +const mongoose = require("mongoose") + +const schema = mongoose.Schema({ + username: String, + email: String, +}) + +module.exports = mongoose.model("User", schema) diff --git a/src/users/users.router.js b/src/users/users.router.js index 139e34a..2f3259c 100644 --- a/src/users/users.router.js +++ b/src/users/users.router.js @@ -2,7 +2,6 @@ const router = require("express").Router({ mergeParams: true }); const controller = require("./users.controller"); const methodNotAllowed = require("../errors/methodNotAllowed"); -router.route("/:userId").get(controller.read).put(controller.update).delete(controller.delete).all(methodNotAllowed); -router.route("/").get(controller.list).post(controller.create).all(methodNotAllowed); +router.route("/").get(controller.list).all(methodNotAllowed); module.exports = router; From ef9cf24a28ce9d5e1fa6910214bff617e827c111 Mon Sep 17 00:00:00 2001 From: gabrielsanchez Date: Tue, 23 Aug 2022 10:03:49 -0500 Subject: [PATCH 2/3] add list function --- src/users/users.controller.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/users/users.controller.js b/src/users/users.controller.js index 133b5d3..516feef 100644 --- a/src/users/users.controller.js +++ b/src/users/users.controller.js @@ -1,7 +1,8 @@ +const User = require("./users.model") async function list(req, res) { - - res.send("users"); + const users = await User.find() + res.send(users); } module.exports = { From a519cb4dabf02f3c457f5b7686bc2f7ee67b66ec Mon Sep 17 00:00:00 2001 From: gabrielsanchez Date: Tue, 30 Aug 2022 11:03:57 -0500 Subject: [PATCH 3/3] change port --- src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index d4ab36d..bd2fb7a 100644 --- a/src/server.js +++ b/src/server.js @@ -1,4 +1,4 @@ -const { PORT = 5000 } = process.env; +const { PORT = 5555 } = process.env; const app = require("./app"); const listener = () => console.log(`Listening on Port ${PORT}!`);