feat(express): new back basic structure

This commit is contained in:
2024-10-16 00:04:23 +02:00
parent 84d8aea1eb
commit 3e6d2b6346
4 changed files with 44 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
import express, { Request, Response } from "express";
import express from "express";
import config from "./config";
import cors from "cors";
import { routes } from "./routes";
const app = express();
const port = config.port;
@@ -9,15 +10,20 @@ if (config.enableCors) {
app.use(cors());
}
app.use((req, res, next) => {
console.log(`${req.method} request for ${req.url}`);
// Global middleware
app.use((req, _res, next) => {
console.log(`LOG: new ${req.method} request for ${req.url}`);
next();
});
app.get("/", (req: Request, res: Response) => {
res.send("Hello, TypeScript Express!");
// Route specific middleware
app.use("/example", (req, _res, next) => {
console.log(`LOG: new ${req.method} request for ${req.url}`);
next();
});
app.use("/api/v1", routes); // / serves as the base for the imported routes
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});