-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (43 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
const express = require('express');
const dotenv = require('dotenv').config();
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
app.set('view engine', 'pug');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use(express.static('pwa'));
app.get('/', function(req, res) {
res.render('index', { title: 'Qui prend quoi ?' });
});
app.post('/party', function(req, res) {
axios
.post(`${process.env.API_URL}/party`, req.body)
.then(({ data }) => res.redirect(`/party/${data._id}`))
.catch((err) => res.send(err));
});
app.get('/party/:id', function(req, res) {
axios
.get(`${process.env.API_URL}/party/${req.params.id}`)
.then(({ data }) =>
res.render('party', {
party: data,
title: data.name,
url: `${process.env.FRONT_URL}:${process.env.PORT}/party/${data._id}`
}),
)
.catch((err) => console.log(err));
});
app.post('/party/:id/items', function(req, res) {
axios
.post(`${process.env.API_URL}/party/${req.params.id}/items`, req.body)
.then(() => res.redirect(`/party/${req.params.id}`))
.catch((err) => res.send(err));
});
app.post('/party/:id/items/:idItem', function(req, res) {
axios
.delete(`${process.env.API_URL}/party/${req.params.id}/items/${req.params.idItem}`)
.then(() => res.redirect(`/party/${req.params.id}`))
.catch((err) => res.send(err));
});
app.listen(process.env.PORT, () => console.log(`Front app listening on port ${process.env.PORT}!`));