-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (48 loc) · 1.66 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
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
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/*Load env variables*/
import dotenv from 'dotenv';
dotenv.config();
/* Declare port */
const PORT = process.env.PORT;
/* Start express */
import express from 'express';
const app = express();
/* Configure dinamic views in ejs */
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/src/views"));
/* Declare static folder */
app.use(express.static(path.join(__dirname, "/public")));
/* POST data */
app.use(express.urlencoded());
app.use(express.json());
/* methodOverride PUT & DELETE */
import methodOverride from 'method-override';
app.use(methodOverride('_method'));
/* Configure Sessions */
import { initSession , userIsLogged , adminIsLogged, cartItems } from './src/middlewares/session.js';
app.use(initSession);
app.use(userIsLogged);
app.use(adminIsLogged);
app.use(cartItems);
/* Allows settion session expire time */
app.use(function (req, res, next) {
req.session.nowInMinutes = Math.floor(Date.now() / 30e3)
return next()
})
/* Set routes */
import { mainRouter } from './src/routes/mainRoutes.js';
import { shopRouter } from './src/routes/shopRoutes.js';
import { adminRouter } from './src/routes/adminRoutes.js';
import { authRouter } from './src/routes/authRoutes.js';
app.use("/", mainRouter);
app.use("/shop", shopRouter);
app.use("/admin", adminRouter);
app.use("/auth", authRouter);
/* Error 404 */
import { error404 } from './src/middlewares/errorHandle.js';
app.use(error404);
/* Start server */
app.listen(PORT, () => console.log(`Servidor corriendo en http://localhost:${PORT}`));