-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
34 lines (29 loc) · 906 Bytes
/
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
let express = require('express');
let app = express();
let ejs = require('ejs');
const haikus = require('./haikus.json');
const port = process.env.PORT || 3000;
const { Pool } = require('pg')
const pool = new Pool({
user: process.env.POSTGRES_USER,
database: process.env.POSTGRES_DB,
password: process.env.POSTGRES_PASSWORD,
host: 'db',
port: 5432
});
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(express.static('public'))
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
pool.query('SELECT * FROM haikus ORDER BY id', (err, haikus) => {
res.render('index', {haikus: haikus.rows});
});
});
app.post('/heart', (req, res) => {
pool.query('UPDATE haikus SET hearts = hearts + 1 WHERE id = $1', [req.body.id], (err, haikus) => {
res.send('Success');
});
});
app.listen(port);
console.log('Server running on','https://localhost:3000')