-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (41 loc) · 1.31 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
import express from 'express';
import pool from './db.js';
const port = 3000;
const app = express();
app.use(express.json())
app.get('/', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM schools')
res.status(200).send(result.rows);
} catch (error) {
console.error('Internal server error: ', error);
res.sendStatus(500);
}
});
app.get('/setup', async (req, res) => {
try {
await pool.query('CREATE TABLE IF NOT EXISTS schools(id serial PRIMARY KEY, name VARCHAR(100), address VARCHAR(100))')
res.status(200).send ({
message: 'Successfully created table'
});
} catch (error) {
console.error('Internal server error: ', error);
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 (error) {
console.error('Internal server error: ', error);
res.sendStatus(500);
}
});
//POST JSON BODY EXAMPLE { "name": "Kid Cudi", "location": "america" }
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});