-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (51 loc) · 1.9 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
const express = require('express');
const app = express();
const path = require('path');
require('dotenv').config();
const mongoose = require('mongoose');
const Viaje = require('./models/viaje');
const port = process.env.PORT || 3000;
let db_url;
if(process.env.MODE_ENV === "local") {
db_url = process.env.LOCAL_URL;
console.log(db_url);
} else {
db_url = process.env.WEB_URL;
console.log(db_url);
}
mongoose.connect(db_url);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
app.set('view engine', 'ejs');
app.set('vews', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true})); // to check req.body.
app.get('/', (req, res) => {
res.render('home');
});
app.get('/viajes', async(req, res) => {
const viajes = await Viaje.find({});
res.render('viajes/index', { viajes });
});
app.get('/viajes/nuevo', (req, res) => {
res.render('viajes/new');
});
app.post('/viajes', async (req, res) => {
// res.send(req.body);
// {"viaje":{"aeronave":"Jet","empresa":"Seplacosa","solicitante":"RC Dalton","f_solicitud":"2022-07-14","salida":"2022-07-30","regreso":"2022-08-07","pasajeros":"fasdfdsa","notas":"fdafds"}}
const viaje = new Viaje(req.body.viaje);
// res.send(viaje);
// {"aeronave":"Jet","empresa":"Seplacosa","solicitante":"RC Dalton","f_solicitud":"2022-07-27T00:00:00.000Z","salida":"2022-07-30T00:00:00.000Z","regreso":"2022-07-30T00:00:00.000Z","pasajeros":"Juan Perez","notas":"Feria del Cafe","_id":"62e1df5d7d536e4db94474ed"}
await viaje.save();
res.redirect(`/viajes/${viaje._id }`)
});
app.get('/viajes/:id', async(req, res) => {
const { id } = req.params;
const viaje = await Viaje.findById(id);
res.render('viajes/show', { viaje });
});
app.listen(port, () => {
console.log(`http://localhost:${port}`);
});