-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
97 lines (87 loc) · 2.45 KB
/
main.js
File metadata and controls
97 lines (87 loc) · 2.45 KB
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
var express = require("express");
var app = express();
var mysql = require("mysql");
var bodyParser = require("body-parser");
// Mysql connection
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "rajan",
database: "nodeapp",
});
connection.connect(function (err) {
if (err) throw err;
console.log("You are now connected...");
});
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// to support URL-encoded bodies
extended: true,
})
);
var server = app.listen(3000, "127.0.0.1", function () {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
app.get("/details/show", function (req, res) {
console.log(req);
connection.query("select * from details", function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
console.log(JSON.stringify(results));
});
});
//NOTE : Here we can also use url.parse to parse the request parameters.....
//rest api to get a single data
app.get("/details/:id", function (req, res) {
connection.query(
"select * from details where id=?",
[req.params.id],
function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
}
);
});
//rest api to create a new record into mysql database
app.post("/details/insert", function (req, res) {
var postData = req.body;
connection.query("INSERT INTO details SET ?", postData, function (
error,
results,
fields
) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
//rest api to update record into mysql database
app.put("/details/update", function (req, res) {
connection.query(
"UPDATE `details` SET `QuoteNumber`=?,``=?,`CustomerName`=?,`SalesPerson`=?,``=?, where `QuoteID`=?",
[
req.body.quotenumber,
req.body.customername,
req.body.salesperson,
req.body.quoteid,
],
function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
}
);
});
//rest api to delete record from mysql database
app.delete("/employees/delete", function (req, res) {
console.log(req.body);
connection.query(
"DELETE FROM `details` WHERE `QuoteID`=?",
[req.body.quoteid],
function (error, results, fields) {
if (error) throw error;
res.end("Record has been deleted!");
}
);
});