-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (104 loc) · 3.06 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const express = require('express')
const app = express()
const ejsMate = require('ejs-mate');
const port = 3000
const bodyParser = require('body-parser');
const { localsName } = require('ejs');
const mongoose = require('mongoose');
const passport = require('passport');
const cookieParser = require('cookie-Parser');
const session = require('express-session')
const Product = require('./models/product');
const User = require('./models/user');
const PassportLocal = require('passport-local').Strategy;
const dbUrl = "mongodb://localhost:27017/agricultureTest";
mongoose.connect(dbUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
//useFindAndModify:false
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
app.use(express.urlencoded({ extended: true}));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // to support URL-encoded bodies
app.use(cookieParser('secreto'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.engine('ejs', ejsMate) //to use a layout file
app.set("view engine", "ejs");
passport.use(new PassportLocal(function(username, password, done){
if (username === 'codigofacilito' && password === '123456789')
return done(null,{ id: 1, name:'Cody' });
done(null, false);
}));
app.use(express.static('public'));
// serializacion
passport.serializeUser(function(user,done){
done(null, user.id);
});
passport.deserializeUser(function(id,done){
done(null,{ id: 1, name:'Cody'});
})
app.get('/', (req, res) => {
res.render('index')
})
app.get('/landing', (req, res) => {
res.render('landing')
})
app.get('/profile', (req, res) => {
res.render('profile');
})
app.get('/settings', (req, res) => {
res.render('settings');
})
app.get('/aboutus', (req, res) => {
res.render('aboutus');
})
app.get('/newProduct', (req, res) => {
res.render('newProduct')
})
app.post('/products', async (req, res) => {
console.log(req.body.product)
const product = new Product(req.body.product);
await product.save();
res.redirect('/')
})
app.post('/products', async (req, res) => {
console.log(req.body.product)
const product = new Product(req.body.product);
await product.save();
res.redirect('/')
})
// getter de pagina de login
app.get('/login', (req, res) => {
res.render('login')
})
app.post("/login", passport.authenticate("local", {
successRedirect: "/landing",
failureRedirect: "/login"
}));
// getter de pagina de registro
app.get('/register', (req, res) => {
res.render('register')
})
app.post('/register', async (req, res) => {
console.log(req.body.user)
const user = new User(req.body.user);
await user.save();
res.redirect('/')
})
app.get('/recover', (req, res) => {
res.render('recover')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})