Singleton, Service pattern for express
This commit is contained in:
@@ -3,29 +3,53 @@ import * as cors from "cors";
|
||||
|
||||
import catsRouter from "./cats/cats.route";
|
||||
|
||||
const app: express.Express = express();
|
||||
const port: number = 8000;
|
||||
class Server {
|
||||
public app: express.Application;
|
||||
|
||||
app.use(cors());
|
||||
constructor() {
|
||||
const app: express.Application = express();
|
||||
this.app = app;
|
||||
}
|
||||
|
||||
// * Logging Middleware
|
||||
app.use((req, res, next) => {
|
||||
console.log(req.rawHeaders[1]);
|
||||
next();
|
||||
});
|
||||
private setRoute() {
|
||||
// * router
|
||||
this.app.use(catsRouter);
|
||||
}
|
||||
|
||||
// * json middleware
|
||||
app.use(express.json());
|
||||
private setMiddleware() {
|
||||
this.app.use(cors());
|
||||
|
||||
app.use(catsRouter);
|
||||
// * Logging Middleware
|
||||
this.app.use((req, res, next) => {
|
||||
console.log(req.rawHeaders[1]);
|
||||
next();
|
||||
});
|
||||
|
||||
// * 404 middleware
|
||||
app.use((req, res, next) => {
|
||||
console.log(req.rawHeaders[1]);
|
||||
console.log("This is 404 middleware");
|
||||
res.send({ error: "404 not found error" });
|
||||
});
|
||||
// * json middleware
|
||||
this.app.use(express.json());
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`server is on ${port}`);
|
||||
});
|
||||
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 { CatType, Cat } from "./cats.model";
|
||||
import {
|
||||
createCat,
|
||||
deleteCat,
|
||||
readAllCat,
|
||||
readCat,
|
||||
updateCat,
|
||||
updatePartialCat,
|
||||
} from "./cats.service";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// * C
|
||||
router.post("/cats", (req, res) => {
|
||||
try {
|
||||
const data = req.body;
|
||||
Cat.push(data);
|
||||
res.status(200).send({
|
||||
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,
|
||||
});
|
||||
}
|
||||
});
|
||||
router.post("/cats", createCat);
|
||||
router.get("/cats", readAllCat);
|
||||
router.get("/cats/:id", readCat);
|
||||
router.put("/cats/:id", updateCat);
|
||||
router.patch("/cats/:id", updatePartialCat);
|
||||
router.delete("/cats/:id", deleteCat);
|
||||
|
||||
export default router;
|
||||
|
120
catDataMocking/src/cats/cats.service.ts
Normal file
120
catDataMocking/src/cats/cats.service.ts
Normal file
@@ -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,
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user