-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (98 loc) · 3.64 KB
/
Copy pathserver.js
File metadata and controls
122 lines (98 loc) · 3.64 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
//require packages
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var fs = require('fs');
var mysql = require('mysql');
//global var
var sendresult = [];
var prodID;
//setup directories for server access
app.use(express.static('public'));
app.use(express.static('node_modules'));
/******************** DB connection *************************/
var con = mysql.createConnection({
host:'localhost',
user:'root',
password:'6470464',
database:'shopDB'
});
/******************** pull catagories *******************/
con.connect(function(err){
if(err) throw err;
con.query("SELECT * FROM categories", function(err, result, fields){
if(err)throw err;
//export data as script file to client side
result = "var catagories= " + JSON.stringify(result);
fs.writeFile("./public/" + 'cat.js', result, "utf8", function(err, data){
if(err){
console.log(err);
}
});
});
});
/******************** let's move it *******************/
//load html
app.get('/', function(req, res){
res.redirect("/index.html");
res.end();
});
/******************** load products by Catagory id *******************/
app.get('/catagories', function(req, res) {
var href = req.query;
con.query('select * from products where categories_id=' + href.product + ';', function(err, result, fields) {
if (err) throw err;
console.log(href.product);
sendresult = result;
io.on('connection', function(socket) {
console.log("Listening To Click")
socket.emit('once', { sendresult: sendresult });
});
});
/******************** sending products to cart *******************/
io.on('connection', function(socket) {
console.log("Listening To cart")
//save prod_id from client
socket.on('addtocart', function(data){
prodID = data.prodID;
//send prod_id to cart with qty 1
con.query('INSERT INTO cart (product_id, qty) values (' + prodID + ', 1);', function(err, result, fields) {
if (err) throw err;
console.log("added product id " + prodID + " to cart");
//add here if else for qty increment
});
});
}); //end connection
/******************** emiting inner join to get full data by cart product id *******************/
con.query('SELECT * FROM products INNER JOIN cart ON products.id = cart.product_id;', function(err, sendresult, fields) {
if (err) throw err;
// console.log("successss");
// console.log(sendresult);
io.on('connection', function(socket) {
console.log("EMITING inner join")
socket.emit('loadCartFromSql', {sendresult:sendresult});
});
});
/******************** building cart filled with products *******************/
io.on('connection', function(socket) {
console.log("gonna fill the cart")
socket.on('loadCartFromSql', function(data){
console.log("im data");
console.log(data);
// prodToCart = data.prodID;
//send prod_id to cart with qty 1
// con.query('select * from cart (product_id);', function(err, result, fields) {
// if (err) throw err;
// console.log("added product id " + prodID + " to cart");
// //add here if else for qty increment
// });
});
}); //end connection
res.redirect("/products.html");
res.end();
})
/******************** rockenroll *******************/
server.listen(8000, function(){
console.log("rockenroll 8000");
});