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/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}!`); 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 = { 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;