Skip to content

Commit fc49461

Browse files
authored
Merge pull request #9 from virus231/feature/auth-endpoints
Update index.ts
2 parents 2a77bb0 + b9bb60a commit fc49461

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

backend/api/index.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1-
import app from '../src/app';
1+
import cors from "cors";
2+
import express from "express";
3+
import helmet from "helmet";
4+
import morgan from "morgan";
5+
import { config } from "../src/config/config";
6+
import { login, register } from "../src/controllers/auth";
27

3-
export default app;
8+
const app = express();
9+
10+
// Security middleware
11+
app.use(helmet());
12+
13+
// CORS configuration
14+
app.use(
15+
cors({
16+
origin: config.corsOrigin,
17+
credentials: true,
18+
})
19+
);
20+
21+
// Logging middleware
22+
app.use(morgan("combined"));
23+
24+
// Body parsing middleware
25+
app.use(express.json({ limit: "10mb" }));
26+
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
27+
28+
// Health check endpoint
29+
app.get("/api/health", (_req, res) => {
30+
res.status(200).json({
31+
status: "OK",
32+
timestamp: new Date().toISOString(),
33+
environment: config.nodeEnv,
34+
});
35+
});
36+
37+
// Auth routes
38+
app.post("/api/auth/register", register);
39+
app.post("/api/auth/login", login);
40+
41+
// API routes
42+
app.use("/api", (_req, res) => {
43+
res.json({ message: "API is working" });
44+
});
45+
46+
// 404 handler
47+
app.use((req, res) => {
48+
res.status(404).json({
49+
error: "Route not found",
50+
message: `Cannot ${req.method} ${req.originalUrl}`,
51+
});
52+
});
53+
54+
// Global error handler
55+
app.use((err: Error, _req: any, res: any, _next: any) => {
56+
console.error("Error:", err);
57+
58+
res.status(500).json({
59+
error: "Internal server error",
60+
message:
61+
config.nodeEnv === "development" ? err.message : "Something went wrong",
62+
});
63+
});
64+
65+
export default app;

0 commit comments

Comments
 (0)