You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.0 KiB

2 weeks ago
import * as express from "express";
import * as cors from "cors";
import catsRouter from "./cats/cats.route";
2 weeks ago
const port: number = 8000;
class Server {
public app: express.Application;
constructor() {
const app: express.Application = express();
this.app = app;
}
private setRoute() {
// * router
this.app.use(catsRouter);
}
private setMiddleware() {
this.app.use(cors());
// * Logging Middleware
this.app.use((req, res, next) => {
console.log(req.rawHeaders[1]);
next();
});
// * 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();