more examples

This commit is contained in:
Mike 2024-12-25 15:27:49 -05:00
parent e3737ba712
commit 12ee817d96
7 changed files with 123 additions and 0 deletions

View file

@ -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!");
});

View file

@ -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 };

View file

@ -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 };

View file

@ -0,0 +1,10 @@
class CustomNotFoundError extends Error {
constructor(message) {
super(message);
this.statusCode = 404;
this.name = "NotFoundError";
}
}
module.exports = CustomNotFoundError;

View file

@ -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;

View file

@ -0,0 +1,7 @@
const { Router } = require("express");
const bookRouter = Router();
bookRouter.get("/", (req, res) => res.send("All Books"));
module.exports = bookRouter;

View file

@ -0,0 +1,7 @@
const { Router } = require("express");
const indexRouter = Router();
indexRouter.get("/", (req, res) => res.send("All Indexes"));
module.exports = indexRouter;