-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (37 loc) · 1.42 KB
/
Copy pathserver.js
File metadata and controls
44 lines (37 loc) · 1.42 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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const port = 3000;
// Enable CORS for all origins
app.use(cors());
app.use(bodyParser.json()); // To parse JSON body requests
// Product Data (You can replace this with a real database or GitHub file)
let productData = {
'Opulent Elegance': { price: 3000, quantity: 50, description: 'A stunning piece', image: 'product1.jpg' },
'Elegant Charm': { price: 1500, quantity: 50, description: 'A classic beauty', image: 'product2.jpg' }
};
// Get Product Data
app.get('/api/products', (req, res) => {
res.json(productData);
});
// Update Product Quantity (after an order is placed)
app.post('/api/update-quantity', (req, res) => {
const { productName, quantityOrdered } = req.body;
if (productData[productName]) {
const newQuantity = productData[productName].quantity - quantityOrdered;
if (newQuantity >= 0) {
productData[productName].quantity = newQuantity;
res.json({ success: true, message: 'Quantity updated', remainingQuantity: newQuantity });
} else {
res.status(400).json({ success: false, message: 'Not enough stock available' });
}
} else {
res.status(404).json({ success: false, message: 'Product not found' });
}
});
// Start the server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});