-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (108 loc) · 4.85 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var async = require('async');
var fs = require("fs");
var path = require('path');
var http = require('http');
var multer = require('multer');
var crypto = require('crypto');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
//=======================================================HTML Pages=====================================================
app.get('/', function (req, res) {
res.sendFile(__dirname + '/views/index.html');
});
//=======================================================API=====================================================
storage = multer.diskStorage({
destination: 'public/images/productImages',
filename: function (req, file, cb) {
return crypto.pseudoRandomBytes(16, function (err, raw) {
if (err) {
return cb(err);
}
var today2 = new Date().getTime();
return cb(null, file.originalname);
});
}
});
app.post("/AddData/:productName/:quantity/:productPrice/:totalPrice", multer({storage: storage}).single('images'), function (req, res) {
console.log("file=====",req.file);
console.log("params=====",req.params);
var obj = {
id: Math.floor(100000 + Math.random() * 900000),//https://stackoverflow.com/questions/21816595/generate-a-random-number-of-fixed-length-using-javascript
productName: req.params.productName,
quantity: req.params.quantity,
productPrice: req.params.productPrice,
totalPrice: req.params.totalPrice,
image: "./images/productImages/" + req.file.filename
}
console.log(obj)
//https://stackoverflow.com/questions/36093042/how-do-i-add-to-an-existing-json-file-in-node-js
fs.readFile('JSON_dataFile.json', function (err, data) {
var json = JSON.parse(data)
json.push(obj)
fs.writeFile("JSON_dataFile.json", JSON.stringify(json), function (err) {
if (err) throw err;
console.log('complete');
fs.readFile('JSON_dataFile.json', function (err, result) {
var json = JSON.parse(data)
console.log(json);
// res.setHeader('Content-Type', 'application/json');//https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express
//res.send(JSON.stringify(json, null, 4));
//res.send(json);
res.statusCode = 302;
res.setHeader("Location", "/");
res.end();
})
});
})
})
app.get("/GetAllData", function(req, res){
fs.readFile('JSON_dataFile.json', function (err, result) {
var json = JSON.parse(result)
console.log(json);
// res.setHeader('Content-Type', 'application/json');//https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express
// res.send(JSON.stringify(json, null, 4));
res.send(json)
})
})
app.get("/Product/:id", function(req, res){
console.log('Parms ' + req.params.id);
//https://stackoverflow.com/questions/36093042/how-do-i-add-to-an-existing-json-file-in-node-js
fs.readFile('JSON_dataFile.json', function (err, data) {
var json = JSON.parse(data)
async.each(json, function (data, callback) {
// Perform operation on file here.
console.log('Processing data ' + data.id);
console.log("====================================================");
// Do work to process file here
if (data.id == req.params.id) {
console.log("Id found")
// res.setHeader('Content-Type', 'application/json');//https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express
// res.send(JSON.stringify(data, null, 4));
res.send(data)
}
console.log('data processed');
console.log("====================================================");
callback();
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A data failed to process');
return;
}
});
})
})
//=========================================================================================================================================
const PORT = process.env.PORT || 3000;
app.listen(PORT);
console.log("Server connected to port" + " " + PORT);