-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (48 loc) · 1.5 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
const express = require("express");
const hbs = require("hbs");
const path = require("path");
require("dotenv").config();
const PORT = 3000;
const fileupload = require("express-fileupload")
const routeLogin = require("./routes/Login");
const routeHome = require("./routes/Home");
const routeMaster = require("./routes/Master");
const routeContact = require("./routes/Contact");
const session = require ("express-session");
const app = express();
app.use(session({
secret: "Proyecto Seguro",
resave: false,
saveUninitialized: true,
}))
const userSecured = async (req,res,next) =>{
if(req.session.user){
next();
}else{
const message = "Usted debe loguearse"
res.render("Login", {message})
}
};
app.use(express.static(path.join(__dirname, "public")));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(fileupload ({
useTempFiles: true,
tempFileDir: "/tmp/"
}))
const localAuth = (req, res, next) => {
app.locals.userLogged = req.session.user
next();}
app.set("view engine", "hbs");
app.set("views", path.join(__dirname, "views"));
hbs.registerPartials(path.join(__dirname,"./views/partials"));
app.use("/", localAuth, routeHome);
app.use("/Login", routeLogin);
app.use("/Master", userSecured, routeMaster);
app.use("/Contact", routeContact);
app.get("*", (req, res) =>{
res.render("wrongRoute");
});
app.listen(PORT, (err) =>{
err? Console.log("Ocurrió un error") : console.log(`Servidor corre en http://localhost:${PORT}`)
});