-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (129 loc) · 4.16 KB
/
server.js
File metadata and controls
148 lines (129 loc) · 4.16 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
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
139
140
141
142
143
144
145
146
147
148
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
var mysql = require("mysql2");
// Connect to MySQL Database (already running) -> Use 'net start MySQL80' to start the server
var connection = mysql.createPool({
connectionLimit: 10,
host: "127.0.0.1",
user: "root",
password: "",
database: "ecommerce",
insecureAuth: true,
});
const app = express();
// Avoid Cross Origin Request errors
var corsOptions = {
origin: "http://localhost:3000",
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// simple route to test connection
app.get("/", (req, res) => {
res.json({ message: "Welcome to webstore application." });
});
app.get("/customers", async (req, res) => {
// Connecting to the database.
connection.getConnection(function (err, connection) {
// Release connection if there is an error
if (err) {
connection.release();
return res.send(400, "Couldn't get a connection");
}
// Executing the MySQL query (select all data from the 'customers' table).
connection.query(
"SELECT * FROM customers",
function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results);
}
);
connection.release();
});
});
app.get("/customers/:username", async (req, res) => {
const { username } = req.params;
connection.getConnection(function (err, connection) {
connection.query(
"SELECT login, password FROM customers WHERE login = ?",
username,
function (error, results, fields) {
if (error) throw error;
res.send(results);
}
);
connection.release();
});
});
app.get("/product/:id", async (req, res) => {
const { id } = req.params;
connection.getConnection(function (err, connection) {
// Release connection if there is an error
if (err) {
connection.release();
return res.send(400, "Couldn't get a connection");
}
connection.query(
`SELECT * FROM products WHERE productID = ${id}`,
function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results);
}
);
connection.release();
});
});
app.get("/products", async (req, res) => {
// Connecting to the database.
connection.getConnection(function (err, connection) {
// Release connection if there is an error
if (err) {
connection.release();
return res.send(400, "Couldn't get a connection");
}
// Executing the MySQL query (select all data from the 'customers' table).
connection.query(
"SELECT * FROM products",
function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results);
}
);
connection.release();
});
});
app.get("/user/:id", async (req, res) => {
const { id } = req.params;
connection.getConnection(function (err, connection) {
// Release connection if there is an error
if (err) {
connection.release();
return res.send(400, "Couldn't get a connection");
}
connection.query(
"SELECT * FROM customers WHERE customerID = ?",
id,
function (error, results, fields) {
// If some error occurs, we throw an error.
if (error) throw error;
// Getting the 'response' from the database and sending it to our route. This is were the data is.
res.send(results);
}
);
connection.release();
});
});
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});