-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
80 lines (58 loc) · 2.11 KB
/
Copy pathapp.ts
File metadata and controls
80 lines (58 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import express from 'express';
import { Request, Response, NextFunction, Application } from 'express';
import morgan from 'morgan';
import helmet from 'helmet';
import xss from 'xss-clean';
import cookieParser from 'cookie-parser';
import { NFIDAuth } from './src/utils/nfidIntegration';
const nfidAuth = new NFIDAuth();
// const compression = require('compression');
import { AppError } from './src/utils/appError';
import globalErrorHandler from './src/controllers/errorController';
import authRouter from './src/routes/authRoutes'
import walletRouter from './src/routes/walletRoutes';
import crossChainRouter from './src/routes/crossChainRoutes';
import liquidityPoolRouter from './src/routes/liquidityPoolRoutes';
const app: Application = express();
// Global MiddleWares
// Security HTTP Headers
app.use(helmet({ contentSecurityPolicy: false }));
// Development logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'));
}
// Body Parser
app.use(express.json({ limit: '10kb' }));
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
app.use(cookieParser());
// Data sanitization against XSS
app.use(xss());
// compress responses
// app.use(compression());
interface CustomRequest extends Request {
requestTime: string; // Add your custom property here
}
// Test middleware
app.use((req: CustomRequest, res: Response, next: NextFunction) => {
req.requestTime = new Date().toISOString();
// console.log(req.cookies);
next();
});
app.use('/api/auth', authRouter);
// Authentication middleware
app.use(async (req, res, next) => {
if (await nfidAuth.isAuthenticated()) {
next();
} else {
res.status(401).json({ success: false, message: "Unauthorized" });
}
});
// Routes
app.use('/api/wallet', walletRouter);
app.use('/api/liquidityPool', liquidityPoolRouter);
app.use('/api/crossChain', crossChainRouter);
app.all('*', (req: Request, res: Response, next: NextFunction) => {
next(new AppError(`Can't find ${req.originalUrl} on this Server!`, 404));
});
app.use(globalErrorHandler);
export default app;