mirror of
https://github.com/andwati/order-service.git
synced 2026-10-29 15:22:16 +00:00
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:
+31
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user