-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (63 loc) · 2.05 KB
/
server.js
File metadata and controls
78 lines (63 loc) · 2.05 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
const express = require("express");
const path = require('path');
const app = express();
const port = process.env.PORT || 5000
const cors = require("cors");
const fs = require("fs");
const url = require("url");
const http = require("http");
app.use(cors());
app.use('/uploads', express.static('public'));
app.use('/product/uploads', express.static('public'));
// app.use('/cart/uploads', express.static('public'));
app.use(express.json({ limit: '1mb' }));
// app.get("/", (req, res) => {
// res.send("Server is running!!!");
// });
app.get("/api/products", (req, res) => {
fs.readFile(__dirname + "/" + "pizza.json", "utf8", (err, data) => {
// console.log(data, err);
res.end(data);
});
});
app.get('/api/products/:version', (req, res) => {
fs.readFile(__dirname + "/" + "pizza.json", "utf8", (err, data) => {
var object = JSON.parse(data);
var product = getProduct(object, req.params.version);
var productObject = JSON.stringify(product);
res.end(productObject);
});
});
function getProduct(x, index) {
var a = x.find(y => y._id === index);
return a;
}
app.post("/api/products/cart-items", (req, res) => {
const cart_ids = req.body.ids;
// console.log(cart_ids);
fs.readFile(__dirname + "/" + "pizza.json", "utf8", (err, data) => {
var object = JSON.parse(data);
var cart = getcartProduct(object, cart_ids);
// console.log(cart);
var cartObject = JSON.stringify(cart);
// console.log(cartObject);
res.end(cartObject);
});
// res.end(JSON.stringify(req.body));
})
function getcartProduct(x, cart_ids) {
const cartproducts = cart_ids.map(id => {
var a = x.find(y => y._id === id);
return a;
})
return (cartproducts);
}
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html')); // relative path
});
}
app.listen(port, function () {
console.log(`Server started on port ${port}`);
});