Skip to content

Commit

Permalink
Add status and refactor parameters from routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcumplido committed Jan 31, 2023
1 parent 8e73e21 commit e3a3871
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion routes/baseURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
31 changes: 14 additions & 17 deletions routes/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 10 additions & 10 deletions routes/poses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});

Expand All @@ -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();
}
});

Expand Down
17 changes: 9 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down

0 comments on commit e3a3871

Please sign in to comment.