-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (132 loc) · 4.59 KB
/
server.js
File metadata and controls
160 lines (132 loc) · 4.59 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
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express');
const mysql = require('mysql');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// MySQL database connection configuration
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'galacticmarketdatabase'
});
app.use('/Gun_Images', express.static('Gun_Images'));
// Connect to MySQL database
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL database:', err);
return;
}
console.log('Connected to MySQL database');
});
// Serve static files from the 'custom-api' directory
app.use(express.static(path.join(__dirname, 'custom-api')));
// Route to serve the HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Route to serve the CSS file
app.get('/styles.css', (req, res) => {
res.sendFile(path.join(__dirname, 'styles.css'));
});
// Route to serve the JavaScript files
app.get('/script.js', (req, res) => {
res.sendFile(path.join(__dirname, 'script.js'));
});
// Route to serve the gunFilter.js file
app.get('/gunFilter.js', (req, res) => {
res.sendFile(path.join(__dirname, 'gunFilter.js'));
});
app.get('/populateGunCards.js', (req, res) => {
res.sendFile(path.join(__dirname, 'populateGunCards.js'));
});
// Route to serve the populateDropdown.js file
app.get('/populateDropdown.js', (req, res) => {
res.sendFile(path.join(__dirname, 'populateDropdown.js'));
});
app.get('/getSize.js', (req, res) => {
res.sendFile(path.join(__dirname, 'getSize.js'));
});
app.get('/api/size', (req, res) => {
const size = ['All', 'Small', 'Medium', 'Large'];
// Logic to fetch size data from your database and send it as a response
res.json(size);
});
app.get('/getColors.js', (req, res) => {
res.sendFile(path.join(__dirname, 'getColors.js'));
});
app.get('/api/colors', (req, res) => {
// Query the database or fetch colors data from wherever it's stored
const colors = ['All', 'Red', 'Black', 'Gray', 'Blue', 'Green', 'Pink', 'Yellow', 'Orange', 'Silver', 'White'];
// Send the colors data as a response
res.json(colors);
});
app.get('/getType.js', (req, res) => {
res.sendFile(path.join(__dirname, 'getType.js'));
});
app.get('/api/type', (req, res) => {
const type = ['All', 'Other', 'Machine Gun', 'Pistol', 'Revolver', 'Rifle', 'Shotgun'];
// Logic to fetch size data from your database and send it as a response
res.json(type);
});
app.get('/getAmmo.js', (req, res) => {
res.sendFile(path.join(__dirname, 'getAmmo.js'));
});
app.get('/api/ammo', (req, res) => {
const ammo = ['All', 'Buckshot', 'Heavy Bullets', 'Flamer Fuel', 'Ice Crystals', 'Laser Crystals', 'Pellets', 'Photon Chargers', 'Pistol Ammo', 'Plasma Pods', "Revolver Bullets", 'Space Battery', 'The Sun(?)'];
// Logic to fetch size data from your database and send it as a response
res.json(ammo);
});
app.get('/getAvailability.js', (req, res) => {
res.sendFile(path.join(__dirname, 'getAvailability.js'));
});
app.get('/api/availability', (req, res) => {
const availability = ['All', 'This product is available.', 'This product is not available.'];
// Logic to fetch size data from your database and send it as a response
res.json(availability);
});
// Route to fetch gun data with filtering
app.get('/api/guns', (req, res) => {
const { size, availability, type, ammo, colors } = req.query;
// Build SQL query dynamically based on provided parameters
let query = 'SELECT * FROM gunstable WHERE 1=1'; // Start with a generic query
const params = [];
// Add conditions for filtering based on provided parameters
if (size && size !== 'All') {
query += ' AND size = ?';
params.push(size);
}
if (availability && availability !== 'All') {
query += ' AND availability = ?';
params.push(availability);
}
if (type && type !== 'All') {
query += ' AND type = ?';
params.push(type);
}
if (ammo && ammo !== 'All') {
query += ' AND ammo = ?';
params.push(ammo);
}
if (colors && colors !== 'All') {
query += ' AND colors = ?';
params.push(colors);
}
// If no filters are applied, return all guns
if (params.length === 0) {
query = 'SELECT * FROM gunstable';
}
// Execute the SQL query with the parameters
connection.query(query, params, (error, results) => {
if (error) {
console.error('Error querying database:', error);
res.status(500).json({ error: 'Internal Server Error' });
return;
}
res.json(results);
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});