-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (45 loc) · 1.47 KB
/
main.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
var express = require('express');
var app = express();
var mysql = require('mysql');
var con = mysql.createConnection({
host: "13.233.70.194",
user: "root",
password: "Root@1234",
database: "bookstore"
});
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.sendFile(__dirname +'/index.html');
});
app.post('/submit-book', function (req, res) {
var bookId = String(req.body.bookId);
var bookName = String(req.body.bookName);
var bookAuthor = String(req.body.bookAuthor);
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = `INSERT INTO books (book_id, book_name, book_author) VALUES (${bookId}, ${bookName}, ${bookAuthor})`;
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
res.send(bookName + ' Added successfully to the database');
});
app.get('/get-book', function(req, res) {
var received;
con.query('SELECT * FROM books', (err,rows) => {
if(err) throw err;
console.log('Data received from Db:');
console.log(rows);
rows = received;
});
res.send(`
<tr>
<th scope="row">${received.book_id}</th>
<td>${received.book_name}</td>
<td>${received.book_author}</td>
</tr>`);
});
app.listen(5000);