-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (52 loc) · 1.37 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
const express = require("express");
const app = express();
const mysql = require("mysql");
require("dotenv").config();
const { insert, read, update, remove } = require("./queries");
const { insertPool } = require("./queries-pool");
app.use(express.json());
const connection = mysql.createConnection({
host: process.env.HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
const pool = mysql.createPool({
host: process.env.HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
connection.connect((err) => {
if (err) throw err;
console.log("Connected to database");
});
app.get("/", (req, res) => {
res.send("Hello express");
});
app.get("/insert", (req, res) => {
insert(connection, { id: 1, name: "diego" }, (result) => {
res.json(result);
});
});
app.get("/insert-pool", (req, res) => {
insertPool(pool, { id: 1, name: "diegoPool" }, (result) => {
res.json(result);
});
});
app.get("/read", (req, res) => {
read(connection, (result) => {
res.json(result);
});
});
app.get("/update", (req, res) => {
update(connection, { id: 1 }, (result) => {
res.json(result);
});
});
app.get("/remove", (req, res) => {
remove(connection, { id: 1 }, (result) => {
res.json(result);
});
});
app.listen(3000, () => console.log("Servidor on port 3000"));