From e3a3871b9f5b36f7ac524d5dc4f7bb496951242a Mon Sep 17 00:00:00 2001 From: Alexandre Cumplido Bou Date: Tue, 31 Jan 2023 17:58:58 +0100 Subject: [PATCH] Add status and refactor parameters from routes --- routes/baseURL.js | 2 +- routes/categories.js | 31 ++++++++++++++----------------- routes/poses.js | 20 ++++++++++---------- server.js | 17 +++++++++-------- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/routes/baseURL.js b/routes/baseURL.js index ddafc05..7cf76c3 100644 --- a/routes/baseURL.js +++ b/routes/baseURL.js @@ -5,7 +5,7 @@ const baseURL = require("../resources/baseURL.json"); const baseUrlRouter = express.Router(); baseUrlRouter.get("/api/yoga/", (request, response) => { - response.json(baseURL); + response.status(200).json(baseURL).end(); }); module.exports = baseUrlRouter; diff --git a/routes/categories.js b/routes/categories.js index 0c32fc0..24546cd 100644 --- a/routes/categories.js +++ b/routes/categories.js @@ -4,28 +4,25 @@ const yogacategories = require("../resources/categories.json"); const categoriesRouter = express.Router(); -categoriesRouter.get("/api/yoga/categories/", (request, response) => { - response.json(yogacategories); +categoriesRouter.get("/api/yoga/categories/", (req, res) => { + res.status(200).json(yogacategories).end(); }); -categoriesRouter.get( - "/api/yoga/category/:categoryName/", - (request, response) => { - const category = request.params.categoryName; - if (isNaN(category)) { - const singleCategory = yogacategories.items.find(function (element) { - return element.name.toLowerCase() === category.toLowerCase(); - }); +categoriesRouter.get("/api/yoga/category/:categoryName/", (req, res) => { + const category = req.params.categoryName; + if (isNaN(category)) { + const singleCategory = yogacategories.items.find(function (element) { + return element.name.toLowerCase() === category.toLowerCase(); + }); - if (singleCategory) { - response.json(singleCategory); - } else { - response.status(404).json({ message: "category not found" }); - } + if (singleCategory) { + res.status(200).json(singleCategory).end(); } else { - response.status(404).json({ message: "non valid request" }); + res.status(404).json({ message: "category not found" }).end(); } + } else { + res.status(404).json({ message: "non valid request" }).end(); } -); +}); module.exports = categoriesRouter; diff --git a/routes/poses.js b/routes/poses.js index 0e4f032..d826da1 100644 --- a/routes/poses.js +++ b/routes/poses.js @@ -4,23 +4,23 @@ const yogaposes = require("../resources/poses.json"); const posesRouter = express.Router(); -posesRouter.get("/api/yoga/poses/", (request, response) => { - response.json(yogaposes); +posesRouter.get("/api/yoga/poses/", (req, res) => { + res.status(200).json(yogaposes).end(); }); -posesRouter.get("/api/yoga/pose/:poseName/", (request, response) => { - const pose = request.params.poseName; +posesRouter.get("/api/yoga/pose/:poseName/", (req, res) => { + const pose = req.params.poseName; if (isNaN(pose)) { const singlePose = yogaposes.items.find(function (element) { return element.english_name.toLowerCase() === pose.toLowerCase(); }); if (singlePose) { - response.json(singlePose); + res.status(200).json(singlePose).end(); } else { - response.status(404).json({ message: "pose not found" }); + res.status(404).json({ message: "pose not found" }).end(); } } else { - response.status(400).json({ message: "non valid request" }); + res.status(400).json({ message: "non valid request" }).end(); } }); @@ -31,12 +31,12 @@ posesRouter.get("/api/yoga/poseId/:id/", (request, response) => { return Number(element.id) === Number(poseId); }); if (poseById) { - response.json(poseById); + response.status(200).json(poseById).end(); } else { - response.status(404).json({ message: "pose not found" }); + response.status(404).json({ message: "pose not found" }).end(); } } else { - response.status(400).json({ message: "non valid request" }); + response.status(400).json({ message: "non valid request" }).end(); } }); diff --git a/server.js b/server.js index a77fb01..a2011c1 100644 --- a/server.js +++ b/server.js @@ -10,18 +10,19 @@ const PORT = process.env.PORT; app.use(cors()); app.use(logger("dev")); +app.get("/", (req, res) => { + res.status(200).sendFile(__dirname + "/index.html"); +}); + +app.use(baseUrlRouter); app.use(posesRouter); app.use(categoriesRouter); -app.use(baseUrlRouter); - -app.get("/", (request, response) => { - response.sendFile(__dirname + "/index.html"); -}); -app.all("*", (request, response) => { - response +app.all("*", (req, res) => { + res .status(404) - .json({ message: "The enpoint for this route does not exist." }); + .json({ message: "The enpoint for this route does not exist." }) + .end(); }); app.listen(PORT || 8000, () => {