-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (100 loc) · 2.96 KB
/
server.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import rateLimit from 'express-rate-limit';
import helmet from 'helmet';
import blockRoutes from './routes/blocks.js';
import transactionRoutes from './routes/transactions.js';
import { validateApiConfig, errorHandler } from './middleware/errorHandler.js';
import { asyncHandler } from './middleware/asyncHandler.js';
if (process.env.NODE_ENV !== 'production') dotenv.config();
const app = express();
const PORT = process.env.PORT || 3001;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = path.join(__dirname, '..');
const securityConfig = {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
scriptSrcAttr: ["'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", 'data:', 'https:'],
fontSrc: ["'self'", 'data:'],
},
},
};
const rateLimitConfig = {
windowMs: 15 * 60 * 1000,
max: 100,
message: { error: 'Too many requests, please try again later.' },
};
const requestLogger = (req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
console.log('API Key present:', !!process.env.BLOCKFROST_API_KEY);
next();
};
// Middleware setup
app.use(cors());
app.use(express.json());
app.use(helmet(securityConfig));
app.use(rateLimit(rateLimitConfig));
// Configure proper MIME types for static files
app.use(
'/js',
express.static(path.join(rootDir, 'public/js'), {
setHeaders: (res, filePath) => {
if (filePath.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
}
},
})
);
app.use(
'/css',
express.static(path.join(rootDir, 'public/css'), {
setHeaders: (res, filePath) => {
if (filePath.endsWith('.css')) {
res.setHeader('Content-Type', 'text/css; charset=utf-8');
}
},
})
);
app.use('/images', express.static(path.join(rootDir, 'public/images')));
// Serve HTML files from root and pages directory
app.use(
express.static(rootDir, {
setHeaders: (res, filePath) => {
if (filePath.endsWith('.html')) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
}
},
})
);
app.use('/api', validateApiConfig);
app.use(requestLogger);
// Route mounting
app.use('/api/blocks', blockRoutes);
app.use('/api/tx', transactionRoutes);
// Development endpoints
if (process.env.NODE_ENV !== 'production') {
app.get('/api/debug', (req, res) => {
res.json({
envVars: {
hasApiKey: !!process.env.BLOCKFROST_API_KEY,
nodeEnv: process.env.NODE_ENV,
},
});
});
}
// Error handler
app.use(errorHandler);
app.listen(PORT, () =>
console.log(`Server running at http://localhost:${PORT}`)
);
// Export the Express API
export default app;