diff --git a/back-express/src/app.ts b/back-express/src/app.ts index 4b6c0bc..d758131 100644 --- a/back-express/src/app.ts +++ b/back-express/src/app.ts @@ -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}`); }); diff --git a/back-express/src/routes/defaultRoute.ts b/back-express/src/routes/defaultRoute.ts new file mode 100644 index 0000000..514887b --- /dev/null +++ b/back-express/src/routes/defaultRoute.ts @@ -0,0 +1,12 @@ +import { Router } from "express"; + +export const defaultRoute = Router(); + +defaultRoute.get("/", (req, res) => { + res.status(400); + res.send("Something"); +}); + +defaultRoute.post("/test", (req, res) => { + res.send("Something with " + JSON.stringify(req.body)); +}); diff --git a/back-express/src/routes/example/example.routes.ts b/back-express/src/routes/example/example.routes.ts new file mode 100644 index 0000000..0b042c8 --- /dev/null +++ b/back-express/src/routes/example/example.routes.ts @@ -0,0 +1,12 @@ +import { Router } from "express"; + +export const exampleRoutes = Router(); + +exampleRoutes.get("/", (req, res) => { + res.status(400); + res.send("Something"); +}); + +exampleRoutes.post("/new", (req, res) => { + res.send("Something with " + JSON.stringify(req.body)); +}); diff --git a/back-express/src/routes/index.ts b/back-express/src/routes/index.ts new file mode 100644 index 0000000..4b52a64 --- /dev/null +++ b/back-express/src/routes/index.ts @@ -0,0 +1,9 @@ +import express from "express"; + +import { defaultRoute } from "./defaultRoute"; +import { exampleRoutes } from "./example/example.routes"; + +export const routes = express.Router(); + +routes.use("/", defaultRoute); +routes.use("/example", exampleRoutes);