-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
146 lines (134 loc) · 3.55 KB
/
server.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const pg = require('pg')
const app = express()
const cors = require('cors')
const PORT = process.env.PORT || 5000
const url = require('url')
try {
require('dotenv').config()
} catch (e) {
console.log(e)
}
const params = url.parse(process.env.DATABASE_URL)
const auth = params.auth.split(':')
const pool = pg.Pool({
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: params.hostname !== 'localhost',
max: 100
})
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(morgan('dev'))
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
next()
})
app.get('/lists', function (req, res) {
pool.connect((error, db, done) => {
if (error) {
return res.status(400).send(error)
} else {
db.query('SELECT * FROM todo ORDER BY ID', (error, table) => {
if (error) {
return res.status(400).send(error)
} else {
res.status(200).send(table.rows)
}
})
}
})
})
app.post('/lists', function (req, res) {
let body = req.body
let type = body.type
let task = body.task
let completed = body.completed
let values = [type, task, completed]
pool.connect((error, db, done) => {
if (error) {
return res.status(400).send(error)
} else {
db.query(
'INSERT INTO todo (type, task, completed) values($1::varchar, $2::text, $3::boolean)',
[...values],
(error, table) => {
if (error) {
return res.status(400).send(error)
} else {
console.log('Data posted.')
db.end()
res.status(201).send({ message: 'Data insterted!' })
}
}
)
}
})
})
app.put('/lists/:id', function (req, res) {
let id = req.body.id
let body = req.body
let type = body.type
let task = body.task
let completed = body.completed
let values = [type, task, completed, id]
pool.connect((error, db, done) => {
if (error) {
return res.status(400).send(error)
} else {
db.query(
'UPDATE todo set type=$1::varchar, task=$2::text, completed=$3::boolean WHERE id=$4::int',
[...values],
(error, table) => {
if (error) {
return res.status(400).send(error)
} else {
console.log('Data updated.')
db.end()
res.status(201).send({ message: 'Data updated!' })
}
}
)
}
})
})
app.delete('/lists/:id', function (req, res) {
let id = req.params.id
pool.connect((error, db, done) => {
if (error) {
return res.status(400).send(error)
} else {
db.query('DELETE FROM todo WHERE id=$1::int', [id], (error, table) => {
if (error) {
return res.status(400).send(error)
} else {
console.log('Data deleted.')
res.status(200).send({ message: 'Data deleted!' })
}
})
}
})
})
app.delete('/lists', function (req, res) {
pool.connect((error, db, done) => {
if (error) {
return res.status(400).send(error)
} else {
db.query('DELETE FROM todo', (error, table) => {
if (error) {
return res.status(400).send(error)
} else {
console.log('All data deleted.')
res.status(200).send({ message: 'All data deleted!' })
}
})
}
})
})
app.listen(PORT, () => console.log('Listening on port: ' + PORT))