feat: initialize Express application with middleware and routes

- Set up Express application in src/app.ts with CORS, JSON parsing, and logging.
- Implement health check route.
- Add authentication, product, and order routes.
- Create error handling middleware for centralized error management.

feat: add authentication middleware with placeholder endpoints

- Implement /register and /login routes in src/middleware/auth.middleware.ts with 501 status responses.

feat: create error handling middleware

- Add error handling middleware in src/middleware/error.middleware.ts to manage errors and send appropriate responses.

feat: implement server startup logic

- Create server entry point in src/server.ts to connect to the database and start the Express server.

feat: add JWT utility functions for token signing and verification

- Implement JWT signing and verification functions in src/utils/jwt.ts with payload interface for user roles.
This commit is contained in:
2026-01-12 23:24:43 +03:00
parent 9e65d2f2d5
commit 95c24b9925
7 changed files with 386 additions and 64 deletions
+31
View File
@@ -0,0 +1,31 @@
import express from "express";
import type { Application } from "express";
import cors from "cors";
import morgan from "morgan";
import { errorHandler } from "./middleware/error.middleware";
import authRoutes from "./routes/auth.routes";
import productRoutes from "./routes/product.routes";
import orderRoutes from "./routes/order.routes";
const app: Application = express();
//global middleware
app.use(cors());
app.use(express.json());
app.use(morgan("dev"));
// health check route
app.get("/health", (req, res) => {
res.status(200).json({ status: "OK" });
});
// routes
app.use("/auth", authRoutes);
app.use("/products", productRoutes);
app.use("/orders", orderRoutes);
// error handling middleware
app.use(errorHandler);
export default app;
+13
View File
@@ -0,0 +1,13 @@
import { Router } from "express";
const router = Router();
router.post("/register", (_req, res) => {
res.status(501).json({ message: "Register endpoint not implemented yet." });
});
router.post("/login", (_req, res) => {
res.status(501).json({ message: "Login endpoint not implemented yet." });
});
export default router;
+18
View File
@@ -0,0 +1,18 @@
import type { Request, Response, NextFunction } from "express";
export function errorHandler(
err: any,
_req: Request,
res: Response,
_next: NextFunction,
) {
console.error(err);
const statusCode = err.statusCode ?? 500;
const message = err.message ?? "Internal Server Error";
res.status(statusCode).json({
error: {
message,
},
});
}
+13
View File
@@ -0,0 +1,13 @@
import app from "./app.js";
import { env } from "./config/env.js";
import { connectDB } from "./config/db.js";
async function startServer() {
await connectDB();
app.listen(env.port, () => {
console.log(`Server is running on port ${env.port}`);
});
}
startServer();
+21
View File
@@ -0,0 +1,21 @@
import * as jwt from "jsonwebtoken";
import { env } from "../config/env.js";
export interface JwtPayload {
sub: string;
role: "admin" | "customer";
}
export function signToken(payload: JwtPayload): string {
return jwt.sign(
payload,
env.jwtSecret.secret as jwt.Secret,
{
expiresIn: env.jwtSecret.expiresIn,
} as jwt.SignOptions,
);
}
export function verifyToken(token: string): JwtPayload {
return jwt.verify(token, env.jwtSecret.secret as jwt.Secret) as JwtPayload;
}