-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (40 loc) · 1.4 KB
/
app.js
File metadata and controls
53 lines (40 loc) · 1.4 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
import express from 'express';
import { engine } from 'express-handlebars';
import path from 'path';
import { fileURLToPath } from 'url';
import { createServer } from 'http';
import { Server } from 'socket.io';
import { connectMongoDB } from './config/db/connect.config.js';
import viewsRouter from './routes/views.router.js';
import homeRouter from './routes/home.router.js';
import cartsRouter from './routes/carts.router.js';
import productsRouter from './routes/products.router.js';
import socketConfig from './socket.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Configuración de Handlebars
app.engine('hbs', engine({ extname: '.hbs'}));
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, 'views'));
// Conexión a Mongo
connectMongoDB();
// Creación del servidor HTTP
const server = createServer(app);
const io = new Server(server);
// Inyectar io en cada request
app.use((req, res, next) => {
req.io = io;
next();
});
app.use('/api/products', productsRouter);
app.use('/api/carts', cartsRouter);
app.use('/', homeRouter);
app.use('/', viewsRouter);
// Sockets
socketConfig(io);
server.listen(PORT, () => console.log(`🚀Servidor corriendo en el puerto ${PORT}`))