38 lines
1 KiB
JavaScript
38 lines
1 KiB
JavaScript
// const express = require('express');
|
|
// const router = express.Router();
|
|
// const Product = require('../models/Product');
|
|
// const auth = require('../middleware/auth');
|
|
|
|
// // GET /api/products - Returns all products (JWT protected)
|
|
// router.get('/', auth, async (req, res) => {
|
|
// try {
|
|
// const products = await Product.find();
|
|
// res.json(products);
|
|
// } catch (err) {
|
|
// res.status(500).json({ error: 'Failed to fetch products' });
|
|
// }
|
|
// });
|
|
|
|
// module.exports = router;
|
|
|
|
|
|
|
|
const express = require('express');
|
|
const router = express.Router();
|
|
const Product = require('../models/Product');
|
|
const authenticateToken = require('../middleware/auth'); // adjust path if needed
|
|
|
|
// GET /api/products?category=Electronics
|
|
router.get('/', authenticateToken, async (req, res) => {
|
|
try {
|
|
const category = req.query.category;
|
|
const filter = category ? { category } : {};
|
|
const products = await Product.find(filter);
|
|
res.json(products);
|
|
} catch (err) {
|
|
res.status(500).json({ error: 'Server error' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|