-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (32 loc) · 876 Bytes
/
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
const express = require("express");
const bodyParser = require('body-parser')
const app = express();
const mysql = require('mysql');
const cors = require('cors');
const db = mysql.createPool({
host: 'localhost',
user: 'sqluser',
password: 'sqluserpw',
database: 'readlist'
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get("/api/get", (req, res) => {
const sqlSelect = "SELECT * FROM books;"
db.query(sqlSelect, (err, result) => {
res.send(result);
});
});
app.post("/api/insert", (req,res) => {
const title = req.body.bookTitle;
const name = req.body.author;
const sqlInsert = "INSERT INTO books (title, name) VALUES(?,?);"
db.query(sqlInsert, [title, name], (err, result) => {
console.log(err);
})
});
// server started
app.listen(5000, () => {
console.log("running on port 5000");
});