-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
138 lines (113 loc) · 3.35 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
const express = require('express');
const path = require('path');
const cors = require('cors');
const app = express();
const port = 4000;
const creds = JSON.parse(process.env.POSTGRES_DATA);
const Pool = require('pg').Pool;
const pool = new Pool({
user: creds.username,
host: creds.host,
database: creds.dbname,
password: creds.password,
port: creds.port,
});
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
const corsOptions = {
origin: "http://localhost:4000"
};
app.use(cors(corsOptions));
//Routers
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get("/todos", async (req, res) => {
try {
const allTodos = await pool.query("SELECT * FROM public.todos");
res.status(200).json(allTodos.rows);
} catch (error) {
console.log(error.meessage);
}
});
app.post("/todos", async (req, res) => {
try {
const { description } = req.body;
const newTodo = await pool.query("INSERT INTO public.todos (description) VALUES($1) RETURNING * ", [description]);
res.status(200).json(newTodo.rows[0]);
} catch (error) {
console.log(error);
}
});
app.get("/todos/:id", async (req, res) => {
try {
const { id } = req.params;
const todo = await pool.query("SELECT * FROM public.todos WHERE todo_id = $1", [id]);
res.status(200).json(todo.rows[0]);
} catch (error) {
console.log(error.message);
}
});
app.put("/todos/:id", async (req, res) => {
try {
const { id } = req.params;
const { description } = req.body;
await pool.query("UPDATE public.todos SET description = $1 WHERE todo_id = $2", [description, id]);
res.status(200).json("Todo updated.");
} catch (error) {
console.log(error);
}
});
app.delete("/todos/:id", async (req, res) => {
try {
const { id } = req.params;
await pool.query("DELETE FROM public.todos WHERE todo_id = $1", [id]);
res.status(200).json("Todo deleted.");
} catch (error) {
console.log(error);
}
});
app.get('/migrate', async (req, res) => {
try {
const create = `
DROP TABLE IF EXISTS todos;
CREATE TABLE todos
(
todo_id SERIAL PRIMARY KEY,
description VARCHAR(255)
);
INSERT INTO public.todos (description) VALUES
(
'Do something excellent today'
);
`;
const resultCreate = await pool.query(create);
res.status(200).send(resultCreate);
} catch (error) {
console.log(error);
}
});
app.get('/env', (req, res) => {
let result = process.env;
res.status(200).json(result);
});
app.get('/creds', (req, res) => {
res.send(`<ul><li>Host is ${creds.host}</li><li>dbname is ${creds.dbname}</li><li>port is ${creds.port}</li><li>password is ${creds.password}</li><li>user is ${creds.username}</li></ul>`);
});
app.get('/parameter-store-demo', (req, res) => {
const envParam = process.env.DEMO_PARAMETER;
if (envParam) {
res.send(`{ "value" : "${envParam}" }`);
} else {
res.send(`{ "value" : "" }`);
}
});
//Start Server
app.listen(port, () => {
console.log(`App running on port ${port}`);
});