feat(express): new back basic structure
This commit is contained in:
@@ -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}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user