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 +2 -7
- package/src/app.ts +38 -0
- package/src/common/middleware/auth.middleware.ts +14 -0
- package/src/config/database.ts +16 -0
- package/src/config/env.ts +0 -0
- package/src/modules/health/health.controller.ts +7 -0
- package/src/modules/health/health.routes.ts +9 -0
- package/src/modules/health/health.service.ts +10 -0
- package/src/modules/health/health.types.ts +6 -0
- package/src/modules/index.ts +14 -0
- package/src/server.ts +7 -0
- package/tsconfig.json +15 -0
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-ts-starter-ys-v1",
|
|
3
|
-
"version": "1.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,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,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
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
|
+
}
|