mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 22:30:44 -05:00
more examples
This commit is contained in:
parent
e3737ba712
commit
12ee817d96
7 changed files with 123 additions and 0 deletions
59
basic-info-node-backend/src/app.js
Normal file
59
basic-info-node-backend/src/app.js
Normal 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!");
|
||||
});
|
18
basic-info-node-backend/src/controllers/authorController.js
Normal file
18
basic-info-node-backend/src/controllers/authorController.js
Normal 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 };
|
11
basic-info-node-backend/src/db.js
Normal file
11
basic-info-node-backend/src/db.js
Normal 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 };
|
10
basic-info-node-backend/src/errors/CustomNotFoundError.js
Normal file
10
basic-info-node-backend/src/errors/CustomNotFoundError.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
class CustomNotFoundError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.statusCode = 404;
|
||||
|
||||
this.name = "NotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CustomNotFoundError;
|
11
basic-info-node-backend/src/routes/authorRouter.js
Normal file
11
basic-info-node-backend/src/routes/authorRouter.js
Normal 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;
|
7
basic-info-node-backend/src/routes/bookRouter.js
Normal file
7
basic-info-node-backend/src/routes/bookRouter.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
const { Router } = require("express");
|
||||
|
||||
const bookRouter = Router();
|
||||
|
||||
bookRouter.get("/", (req, res) => res.send("All Books"));
|
||||
|
||||
module.exports = bookRouter;
|
7
basic-info-node-backend/src/routes/indexRouter.js
Normal file
7
basic-info-node-backend/src/routes/indexRouter.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
const { Router } = require("express");
|
||||
|
||||
const indexRouter = Router();
|
||||
|
||||
indexRouter.get("/", (req, res) => res.send("All Indexes"));
|
||||
|
||||
module.exports = indexRouter;
|
Loading…
Reference in a new issue