-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (94 loc) · 3.16 KB
/
server.js
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
//npm install express
var express = require('express');
//npm install body-parser
var bodyParser = require('body-parser');
var app = express();
var fs = require("fs");
var router = express.Router();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use('/api', router);
var category = require('./category');
var product = require('./product');
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
next(); // make sure we go to the next routes and don't stop here
});
////////////////////////////////////////////////////////////////////////////////////
//////////////////////// Category //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
router.get('/listCategories', function (req, res) {
category.getAllCategory(function(err,data) {
console.log(data);
res.send(data);
});
});
router.post('/getCategory', function(req, res) {
category.getCategory(req.body.categoryid, function(err,data) {
res.send(data);
});
});
router.post('/addCategory', function(req, res) {
category.insertCategory(req.body, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
router.put('/editCategory', function(req, res) {
category.updateCategory(req.body, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
router.delete('/deleteCategory', function(req, res) {
category.removeCategory(req.body.categoryid, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
////////////////////////////////////////////////////////////////////////////////////
//////////////////////// Product //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
router.get('/listProducts', function (req, res) {
product.getAllProduct(function(err,data) {
console.log(data);
res.send(data);
});
})
router.post('/getProduct', function(req, res) {
product.getProduct(req.body.productid, function(err,data) {
res.send(data);
});
});
router.post('/addProduct', function(req, res) {
product.insertProduct(req.body, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
router.put('/editProduct', function(req, res) {
product.updateProduct(req.body, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
router.delete('/deleteProduct', function(req, res) {
product.removeProduct(req.body.productid, function(err,data) {
res.send(
(err === null) ? { msg: 'success' } : { msg: err }
);
});
});
var server = app.listen(8085, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})