Singleton, Service pattern for express

main
Peace 2 weeks ago
parent b87d40ade8
commit c8b48004f0
  1. 74
      catDataMocking/src/app.ts
  2. 131
      catDataMocking/src/cats/cats.route.ts
  3. 120
      catDataMocking/src/cats/cats.service.ts

@ -3,29 +3,53 @@ import * as cors from "cors";
import catsRouter from "./cats/cats.route"; import catsRouter from "./cats/cats.route";
const app: express.Express = express();
const port: number = 8000; const port: number = 8000;
class Server {
app.use(cors()); public app: express.Application;
// * Logging Middleware constructor() {
app.use((req, res, next) => { const app: express.Application = express();
console.log(req.rawHeaders[1]); this.app = app;
next(); }
});
private setRoute() {
// * json middleware // * router
app.use(express.json()); this.app.use(catsRouter);
}
app.use(catsRouter);
private setMiddleware() {
// * 404 middleware this.app.use(cors());
app.use((req, res, next) => {
console.log(req.rawHeaders[1]); // * Logging Middleware
console.log("This is 404 middleware"); this.app.use((req, res, next) => {
res.send({ error: "404 not found error" }); console.log(req.rawHeaders[1]);
}); next();
});
app.listen(port, () => {
console.log(`server is on ${port}`); // * json middleware
}); this.app.use(express.json());
this.setRoute();
// * 404 middleware
this.app.use((req, res, next) => {
console.log(req.rawHeaders[1]);
console.log("This is 404 middleware");
res.send({ error: "404 not found error" });
});
}
public listen() {
this.setMiddleware();
this.app.listen(port, () => {
console.log(`server is on ${port}`);
});
}
}
function init() {
const server = new Server();
server.listen();
}
init();

@ -1,124 +1,21 @@
import { Router } from "express"; import { Router } from "express";
import { CatType, Cat } from "./cats.model"; import { CatType, Cat } from "./cats.model";
import {
createCat,
deleteCat,
readAllCat,
readCat,
updateCat,
updatePartialCat,
} from "./cats.service";
const router = Router(); const router = Router();
// * C router.post("/cats", createCat);
router.post("/cats", (req, res) => { router.get("/cats", readAllCat);
try { router.get("/cats/:id", readCat);
const data = req.body; router.put("/cats/:id", updateCat);
Cat.push(data); router.patch("/cats/:id", updatePartialCat);
res.status(200).send({ router.delete("/cats/:id", deleteCat);
success: true,
data: { data },
});
} catch (error) {}
});
// * R
router.get("/cats", (req, res) => {
try {
const cats = Cat;
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
router.get("/cats/:id", (req, res) => {
try {
const params = req.params;
const cats = Cat.find((cat) => {
return cat.id === params.id;
});
if (!cats) throw new Error("no matched data");
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
// * U
router.put("/cats/:id", (req, res) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
const tempId = params.id;
const newCat = { ...body, id: tempId };
Cat[index] = newCat;
result = newCat;
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
router.patch("/cats/:id", (req, res) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
const tempId = params.id;
const newCat = { ...Cat[index], ...body, id: tempId };
Cat[index] = newCat;
result = newCat;
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
// * D
router.delete("/cats/:id", (req, res) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
Cat.splice(index, 1);
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
export default router; export default router;

@ -0,0 +1,120 @@
import { Request, Response } from "express";
import { CatType, Cat } from "./cats.model";
// * C
export const createCat = (req: Request, res: Response) => {
try {
const data = req.body;
Cat.push(data);
res.status(200).send({
success: true,
data: { data },
});
} catch (error) {}
};
// * R
export const readAllCat = (req: Request, res: Response) => {
try {
const cats = Cat;
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
};
export const readCat = (req: Request, res: Response) => {
try {
const params = req.params;
const cats = Cat.find((cat) => {
return cat.id === params.id;
});
if (!cats) throw new Error("no matched data");
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
};
// * U
export const updateCat = (req: Request, res: Response) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
const tempId = params.id;
const newCat = { ...body, id: tempId };
Cat[index] = newCat;
result = newCat;
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
};
export const updatePartialCat = (req: Request, res: Response) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
const tempId = params.id;
const newCat = { ...Cat[index], ...body, id: tempId };
Cat[index] = newCat;
result = newCat;
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
};
// * D
export const deleteCat = (req: Request, res: Response) => {
try {
const params = req.params;
const body = req.body;
let result;
const index = Cat.findIndex((cat) => cat.id === params.id);
if (index !== -1) {
Cat.splice(index, 1);
}
res.status(200).send({
success: true,
data: { cat: result },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
};
Loading…
Cancel
Save