From 12ee817d96ce10da47d60279480489fdfcd31a3c Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Wed, 25 Dec 2024 15:27:49 -0500 Subject: [PATCH] more examples --- basic-info-node-backend/src/app.js | 59 +++++++++++++++++++ .../src/controllers/authorController.js | 18 ++++++ basic-info-node-backend/src/db.js | 11 ++++ .../src/errors/CustomNotFoundError.js | 10 ++++ .../src/routes/authorRouter.js | 11 ++++ .../src/routes/bookRouter.js | 7 +++ .../src/routes/indexRouter.js | 7 +++ 7 files changed, 123 insertions(+) create mode 100644 basic-info-node-backend/src/app.js create mode 100644 basic-info-node-backend/src/controllers/authorController.js create mode 100644 basic-info-node-backend/src/db.js create mode 100644 basic-info-node-backend/src/errors/CustomNotFoundError.js create mode 100644 basic-info-node-backend/src/routes/authorRouter.js create mode 100644 basic-info-node-backend/src/routes/bookRouter.js create mode 100644 basic-info-node-backend/src/routes/indexRouter.js diff --git a/basic-info-node-backend/src/app.js b/basic-info-node-backend/src/app.js new file mode 100644 index 0000000..b236389 --- /dev/null +++ b/basic-info-node-backend/src/app.js @@ -0,0 +1,59 @@ +const express = require("express"); +const fs = require("fs"); +const app = express(); +const authorRouter = require("./routes/authorRouter"); +const bookRouter = require("./routes/bookRouter"); +const indexRouter = require("./routes/indexRouter"); + +app.use("/authors", authorRouter); +app.use("/books", bookRouter); + +app.get("/", (req, res) => { + let filename = + "/home/smig/repos/github/odin-codeprojects/basic-info-node-backend/src/content/index.html"; + res.sendFile(filename, (err) => { + if (err) { + res.send("err"); + } else { + console.log("sent", filename); + } + }); +}); + +app.get("/about", (req, res) => { + res.sendFile( + "/home/smig/repos/github/odin-codeprojects/basic-info-node-backend/src/content/about.html", + (err) => { + if (err) res.status(404).send("err", err); + }, + ); +}); + +app.get("/contact-me", (req, res) => { + res.sendFile( + "/home/smig/repos/github/odin-codeprojects/basic-info-node-backend/src/content/contact.html", + (err) => { + if (err) res.send("err:", err); + }, + ); +}); + +app.use((req, res, next) => { + console.log("Another middleware"); + res.send("Response from this middleware"); +}); + +app.use((req, res, next) => { + throw new Error("OH NO!"); +}); + +app.use((err, req, res, ext) => { + console.error(err); + res.status(err.statuscode || 500).send(err.message); +}); + +const PORT = 3000; + +app.listen(PORT, () => { + console.log("My first time running express baby!"); +}); diff --git a/basic-info-node-backend/src/controllers/authorController.js b/basic-info-node-backend/src/controllers/authorController.js new file mode 100644 index 0000000..a35a139 --- /dev/null +++ b/basic-info-node-backend/src/controllers/authorController.js @@ -0,0 +1,18 @@ +const asyncHandler = require("express-async-handler"); + +const db = require("../db"); +const CustomNotFoundError = require("../errors/CustomNotFoundError"); + +const getAuthorById = asyncHandler(async (req, res) => { + const { authorId } = req.params; + + const author = await db.getAuthorById(Number(authorId)); + + if (!author) { + throw new CustomNotFoundError("Author not found"); + } + + res.send(`Author name: ${author.name}`); +}); + +module.exports = { getAuthorById }; diff --git a/basic-info-node-backend/src/db.js b/basic-info-node-backend/src/db.js new file mode 100644 index 0000000..8a81a0a --- /dev/null +++ b/basic-info-node-backend/src/db.js @@ -0,0 +1,11 @@ +const authors = [ + { id: 1, name: "Bob" }, + { id: 2, name: "Jane" }, + { id: 3, name: "Fred" }, +]; + +async function getAuthorById(authorId) { + return authors.find((author) => author.id === authorId); +} + +module.exports = { getAuthorById }; diff --git a/basic-info-node-backend/src/errors/CustomNotFoundError.js b/basic-info-node-backend/src/errors/CustomNotFoundError.js new file mode 100644 index 0000000..f9a988b --- /dev/null +++ b/basic-info-node-backend/src/errors/CustomNotFoundError.js @@ -0,0 +1,10 @@ +class CustomNotFoundError extends Error { + constructor(message) { + super(message); + this.statusCode = 404; + + this.name = "NotFoundError"; + } +} + +module.exports = CustomNotFoundError; diff --git a/basic-info-node-backend/src/routes/authorRouter.js b/basic-info-node-backend/src/routes/authorRouter.js new file mode 100644 index 0000000..12e6193 --- /dev/null +++ b/basic-info-node-backend/src/routes/authorRouter.js @@ -0,0 +1,11 @@ +const { Router } = require("express"); + +const { getAuthorById } = require("../controllers/authorController"); + +const authorRouter = Router(); + +authorRouter.get("/", (req, res) => res.send("All Authors")); + +authorRouter.get("/:authorId", getAuthorById); + +module.exports = authorRouter; diff --git a/basic-info-node-backend/src/routes/bookRouter.js b/basic-info-node-backend/src/routes/bookRouter.js new file mode 100644 index 0000000..6967039 --- /dev/null +++ b/basic-info-node-backend/src/routes/bookRouter.js @@ -0,0 +1,7 @@ +const { Router } = require("express"); + +const bookRouter = Router(); + +bookRouter.get("/", (req, res) => res.send("All Books")); + +module.exports = bookRouter; diff --git a/basic-info-node-backend/src/routes/indexRouter.js b/basic-info-node-backend/src/routes/indexRouter.js new file mode 100644 index 0000000..6b3de21 --- /dev/null +++ b/basic-info-node-backend/src/routes/indexRouter.js @@ -0,0 +1,7 @@ +const { Router } = require("express"); + +const indexRouter = Router(); + +indexRouter.get("/", (req, res) => res.send("All Indexes")); + +module.exports = indexRouter;