-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
54 lines (43 loc) · 2.29 KB
/
server.js
File metadata and controls
54 lines (43 loc) · 2.29 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
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const cors = require('cors');
const app = express();
const port = 3001;
app.use(cors());
app.use(bodyParser.json());
app.use(express.static('public'));
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'arthmohan',
database: 'kai'
});
db.connect((err) => {
if (err) throw err;
console.log('Connected to the database');
});
let latestResults = [];
app.post('/data', (req, res) => {
let { skinTypeResults, skinConcernsResults, priceRangeResults } = req.body;
// Replace empty arrays with an array that contains a value that doesn't match any records
skinTypeResults = skinTypeResults.length ? skinTypeResults : ['no_match'];
skinConcernsResults = skinConcernsResults.length ? skinConcernsResults : ['no_match'];
priceRangeResults = priceRangeResults.length ? priceRangeResults : ['no_match'];
let skinTypePlaceholders = skinTypeResults.map(() => '?').join(',');
let skinConcernsPlaceholders = skinConcernsResults.map(() => '?').join(',');
let priceRangePlaceholders = priceRangeResults.map(() => '?').join(',');
let sql = `SELECT p.Image_Link, p.name AS product_name, p.category, p.price_range, p.mrp, GROUP_CONCAT(DISTINCT st.skin_type_name ORDER BY st.skin_type_name ASC SEPARATOR ', ') AS skin_types, GROUP_CONCAT(DISTINCT sc.skin_concern_name ORDER BY sc.skin_concern_name ASC SEPARATOR ', ') AS skin_concerns, p.Purchase_Link FROM Products p INNER JOIN Product_Skin_Types pst ON p.product_id = pst.product_id INNER JOIN Skin_Types st ON pst.skin_type_id = st.skin_type_id LEFT JOIN Product_Skin_Concerns psc ON p.product_id = psc.product_id LEFT JOIN Skin_Concerns sc ON psc.skin_concern_id = sc.skin_concern_id WHERE st.skin_type_name IN (${skinTypePlaceholders}) AND sc.skin_concern_name IN (${skinConcernsPlaceholders}) AND p.price_range IN (${priceRangePlaceholders}) GROUP BY p.product_id;`;
db.query(sql, [...skinTypeResults, ...skinConcernsResults, ...priceRangeResults], (err, results) => {
if (err) throw err;
latestResults = results;
res.json(results);
});
});
app.get('/data', (req, res) => {
res.json(latestResults);
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});