-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (56 loc) · 1.91 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
#!/usr/bin/env nodejs
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT
const bodyParser = require('body-parser')
const fs = require('fs')
const uuidv4 = require('uuid/v4')
const { Client } = require('pg')
app.set('view engine', 'pug')
app.set('views', './views')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({limit: '5mb'}))
app.get('/', (req, res) => {
res.render('index')
})
app.get('/images', (req, res) => {
const client = new Client({
database: process.env.DATABASE,
host: 'localhost',
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: process.env.DBPORT
})
client.connect().catch(err => console.log('Error:', err))
client.query('SELECT image FROM complaints')
.then(result => {
res.render('images', {images: result.rows})
})
.catch(err => console.log(err))
})
app.get('/posted', (req, res) => {
res.render('posted')
})
app.post('/post', (req, res) => {
console.log('reached')
const client = new Client({
database: process.env.DATABASE,
host: 'localhost',
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: process.env.DBPORT
})
client.connect().catch(err => console.log('Error:', err))
var base64Data = req.body.image.replace(/^data:image\/png;base64,/, "");
var name = uuidv4()
fs.writeFile(`public/images/${name}.png`, base64Data, 'base64', function(err) {
if (err) console.log('Error:', err)
})
client.query(`INSERT INTO complaints (name, postcode, image, email) VALUES ('${req.body.name}', '${req.body.postcode}', '${name}', '${req.body.email}')`)
.catch(err => console.log('Error:', err))
res.status(200).send({message: 'Posted!'})
})
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})