express-ts-starter-ys-v1 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "express-ts-starter-ys-v1",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/server.js",
6
6
  "types": "dist/server.d.ts",
7
- "files": [
8
- "dist"
9
- ],
10
7
  "scripts": {
11
8
  "prepublishOnly": "npm run build",
12
9
  "build": "tsc",
@@ -23,9 +20,7 @@
23
20
  "dotenv": "^18.0.7",
24
21
  "express": "^5.3.0",
25
22
  "helmet": "^8.3.0",
26
- "mongoose": "^9.11.1",
27
- "pino": "^10.4.0",
28
- "pino-pretty": "^13.2.0"
23
+ "mongoose": "^9.11.1"
29
24
  },
30
25
  "devDependencies": {
31
26
  "@types/cors": "^2.8.19",
package/src/app.ts ADDED
@@ -0,0 +1,38 @@
1
+ import express, { Application, Request, Response } from 'express';
2
+ import dotenv from 'dotenv';
3
+ import cors from 'cors';
4
+ import helmet from 'helmet';
5
+ import { moduleRoutes } from './modules';
6
+
7
+
8
+ dotenv.config();
9
+
10
+ const app: Application = express();
11
+
12
+ // Middlewares
13
+ app.use(helmet({
14
+ contentSecurityPolicy: false,
15
+ crossOriginEmbedderPolicy: false,
16
+ }));
17
+ app.use(cors({
18
+ origin: '*',
19
+ methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
20
+ }));
21
+ app.use(express.json());
22
+
23
+ // Base Route
24
+ app.get('/', (req: Request, res: Response) => {
25
+ res.status(200).json({ message: 'API is running' });
26
+ });
27
+
28
+ // Module Routes
29
+ moduleRoutes.forEach((route) => {
30
+ app.use(route.path, route.route);
31
+ });
32
+
33
+ // 404 Fallback Handler
34
+ app.use((_req: Request, res: Response) => {
35
+ res.status(404).json({ message: 'Not found' });
36
+ });
37
+
38
+ export default app;
@@ -0,0 +1,14 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+
3
+ export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
4
+ // Placeholder authentication logic
5
+ const authHeader = req.headers.authorization;
6
+
7
+ if (!authHeader) {
8
+ return res.status(401).json({ message: 'Unauthorized: No token provided' });
9
+ }
10
+
11
+ // You would typically verify your JWT or token here.
12
+ // For now, we'll just allow it to pass through.
13
+ next();
14
+ };
@@ -0,0 +1,16 @@
1
+
2
+ import mongoose from 'mongoose';
3
+
4
+ export const connectDatabase = async (): Promise<void> => {
5
+ try {
6
+ const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017/my-api';
7
+
8
+ await mongoose.connect(mongoURI);
9
+
10
+ console.log('MongoDB Connected Successfully');
11
+ } catch (error) {
12
+ console.error('Error connecting to MongoDB:', error);
13
+ // Exit process with failure
14
+ process.exit(1);
15
+ }
16
+ };
File without changes
@@ -0,0 +1,7 @@
1
+ import { Request, Response } from 'express';
2
+ import { getHealthStatus } from './health.service';
3
+
4
+ export const checkHealth = (req: Request, res: Response) => {
5
+ const healthData = getHealthStatus();
6
+ res.status(200).json(healthData);
7
+ };
@@ -0,0 +1,9 @@
1
+ import { Router } from 'express';
2
+ import { checkHealth } from './health.controller';
3
+ import { authMiddleware } from '../../common/middleware/auth.middleware';
4
+
5
+ const router = Router();
6
+
7
+ router.get('/', authMiddleware, checkHealth);
8
+
9
+ export default router;
@@ -0,0 +1,10 @@
1
+ import { HealthStatus } from './health.types';
2
+
3
+ export const getHealthStatus = (): HealthStatus => {
4
+ return {
5
+ status: 'OK',
6
+ message: 'API is healthy',
7
+ timestamp: new Date().toISOString(),
8
+ uptime: process.uptime()
9
+ };
10
+ };
@@ -0,0 +1,6 @@
1
+ export interface HealthStatus {
2
+ status: string;
3
+ message: string;
4
+ timestamp: string;
5
+ uptime: number;
6
+ }
@@ -0,0 +1,14 @@
1
+ import { Router } from 'express';
2
+ import healthRoutes from "./health/health.routes";
3
+
4
+ export interface ModuleRoute {
5
+ path: string;
6
+ route: Router;
7
+ }
8
+
9
+ export const moduleRoutes: ModuleRoute[] = [
10
+ {
11
+ path: '/health',
12
+ route: healthRoutes,
13
+ },
14
+ ];
package/src/server.ts ADDED
@@ -0,0 +1,7 @@
1
+ import app from './app';
2
+
3
+ const PORT = process.env.PORT || 3000;
4
+
5
+ app.listen(PORT, () => {
6
+ console.log(`Server is running on port ${PORT}`);
7
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "commonjs",
5
+ "rootDir": "./src",
6
+ "outDir": "./dist",
7
+ "esModuleInterop": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "strict": true,
10
+ "skipLibCheck": true,
11
+ "declaration": true,
12
+ "declarationMap": true
13
+ },
14
+ "include": ["src/**/*"]
15
+ }