Advertisement
Guest User

Untitled

a guest
Apr 16th, 2025
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. require('dotenv').config();
  2.  
  3. import express, { Application, Router } from 'express';
  4. import morgan from 'morgan';
  5. import cors from 'cors';
  6. import cookieParser from 'cookie-parser';
  7. import helmet from 'helmet';
  8. import rateLimit from 'express-rate-limit';
  9. import fs from 'fs';
  10. import path from 'path';
  11. import { toNodeHandler } from "better-auth/node";
  12. import { auth } from "./config/auth";
  13.  
  14. const app: Application = express();
  15. const port = process.env.PORT || 8080;
  16.  
  17. app.use(cookieParser());
  18. app.use(morgan('combined'));
  19. app.use(
  20. cors({
  21. origin: "http://localhost:3000",
  22. methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
  23. credentials: true,
  24. })
  25. );
  26. app.all("/api/auth/*splat", toNodeHandler(auth));
  27.  
  28. app.use(rateLimit({
  29. windowMs: 60 * 1000,
  30. max: 50,
  31. message: 'Too many requests from this IP, please try again later',
  32. }));
  33.  
  34. app.use(helmet());
  35. app.use(helmet.xssFilter());
  36. app.use(helmet.frameguard({ action: 'deny' }));
  37. app.use(helmet.hsts({
  38. maxAge: 31536000,
  39. includeSubDomains: true,
  40. preload: true,
  41. }));
  42.  
  43. app.use(express.json());
  44.  
  45.  
  46. function loadRoutes(app: Application): void {
  47. const routesDir = path.resolve(__dirname, './routes');
  48. fs.readdirSync(routesDir).forEach(file => {
  49. const filePath = path.join(routesDir, file);
  50. if (fs.statSync(filePath).isFile() && filePath.endsWith('.ts')) {
  51. import(filePath).then(routeModule => {
  52. const route: Router = routeModule.default || routeModule;
  53. app.use(route);
  54. }).catch(err => {
  55. console.error(`Error loading route ${filePath}:`, err);
  56. })
  57. }
  58. })
  59. }
  60.  
  61. loadRoutes(app);
  62.  
  63. app.use(express.json({ limit: '50mb' }));
  64. app.use(express.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));
  65.  
  66. const server = app.listen(port, () => {
  67. console.log(`Server up and running on port ${port}`);
  68. })
  69.  
  70. process.on('unhandledRejection', (err: Error) => {
  71. console.error(`Unhandled Rejection: ${err.message}`);
  72. server.close(() => process.exit(1));
  73. })
  74.  
  75. export default app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement