-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (70 loc) · 2.19 KB
/
app.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
const express = require('express');
const http = require('http');
const { Server } = require('ws');
const connectDB = require('./config/db');
const dotenv = require('dotenv');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const csrf = require('csrf');
const path = require('path');
const rateLimit = require('./middleware/rateLimit');
dotenv.config();
const app = express();
const server = http.createServer(app);
const wss = new Server({ server });
app.set('trust proxy', 1);
connectDB();
app.use(helmet());
app.disable('x-powered-by');
app.use(express.json());
app.use(cookieParser());
const tokens = new csrf();
const csrfSecret = process.env.CSRF_SECRET;
app.use((req, res, next) => {
if (req.method === 'GET') {
const token = tokens.create(csrfSecret);
res.cookie('csrf-token', token);
res.locals.csrfToken = token;
} else {
const token = req.cookies['csrf-token'];
try {
tokens.verify(csrfSecret, token);
} catch (e) {
return res.status(403).send('Form tampered with.');
}
}
next();
});
app.use((err, req, res, next) => {
if (err.code === 'EBADCSRFTOKEN') {
res.status(403).send('Form tampered with.');
} else {
next(err);
}
});
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.use(express.static(path.join(__dirname, 'public')));
const { upload } = require('./middleware/upload');
app.use('/api/books', rateLimit, require('./routes/books'));
app.use('/api/auth', rateLimit, require('./routes/auth'));
app.get('/form', (req, res) => {
res.json({ csrfToken: res.locals.csrfToken });
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Manejar conexiones WebSocket
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('message', (message) => {
console.log(`Received: ${message}`);
});
ws.on('close', () => {
console.log('Client disconnected');
});
// Enviar estado inicial
ws.send(JSON.stringify({ status: 'OK', message: 'Welcome to Biblioteca API WebSocket' }));
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
module.exports = app;