-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (35 loc) · 1.08 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
const express = require('express')
const pool = require('./db')
const port = 3000
const app = express()
app.use(express.json())
//routes
app.get('/', async (req, res) => {
try {
const data = await pool.query('SELECT * FROM schools')
res.status(200).send(data.rows)
} catch (err) {
console.log(err)
res.sendStatus(500)
}
})
app.post('/', async (req, res) => {
const { name, location } = req.body
try {
await pool.query('INSERT INTO schools (name, address) VALUES ($1, $2)', [name, location])
res.status(200).send({ message: "Successfully added child" })
} catch (err) {
console.log(err)
res.sendStatus(500)
}
})
app.get('/setup', async (req, res) => {
try {
await pool.query('CREATE TABLE schools( id SERIAL PRIMARY KEY, name VARCHAR(100), address VARCHAR(100))')
res.status(200).send({ message: "Successfully created table" })
} catch (err) {
console.log(err)
res.sendStatus(500)
}
})
app.listen(port, () => console.log(`Server has started on port: ${port}`))