-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (27 loc) · 1.04 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const authRoutes = require('./routes/auth');
const postsRoutes = require('./routes/posts');
const userRoutes = require('./routes/userRoutes');
const errorController = require('./controllers/error');
const app = express();
const ports = process.env.PORT || 3000;
app.use(bodyParser.json());
// Configuración de CORS
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept, X-Custom-Header, Authorization');
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
next();
});
// Define tus rutas después de configurar CORS
app.use('/api/users', userRoutes);
app.use('/auth', authRoutes);
app.use('/post', postsRoutes);
app.use(errorController.get404);
app.use(errorController.get500);
app.listen(ports, () => console.log(`Listening on port ${ports}`));