Description: verifyAccessToken use this middleware function in all router. import it…
- User
Description: verifyAccessToken use this middleware function in all router. import it from ../middlewares/auth
// Import necessary packages and modules const express = require('express'); const { readSingleProduct, readAllProducts, createProduct, updateProduct, deleteProduct, addToWhishlist, deleteProductFromWishlist } = require('../controllers/productController'); // Adjust path to your controller const { param, body } = require('express-validator');
const router = express.Router();
// Middleware for validation const validateProduct = [ body('name').isString().notEmpty(), body('description').isString(), body('price').isNumeric().notEmpty(), body('sizes').isArray(), body('colors').isArray(), body('images').isArray(), body('stock').isNumeric().notEmpty(), body('rating').isNumeric().optional() ];
const validateProductId = [ param('id').isMongoId().withMessage('Invalid product ID') ];
// Routes router.get('/products', readAllProducts); router.get('/products/:id', validateProductId, readSingleProduct); router.post('/products', validateProduct, createProduct); router.put('/products/:id', validateProduct, validateProductId, updateProduct); router.delete('/products', deleteProduct); router.post('/addtowhish', addToWhishlist); router.delete('/deletewhish', deleteProductFromWishlist);
// Additional routes for PUT, PATCH, and DELETE can be defined here
module.exports = router;
Generate source code based on the description above and respond in the following JSON format ONLY: ```json { "name": "prompt_name", "schema": { "type": "string", "code": "Generated code here" } } ``` IMPORTANT: - Ensure the JSON response is error-free. - Include no additional text or explanations. Respond with the JSON object only. - Assistant
{ "name": "productRoutes", "schema": { "type": "string", "code": "// Import necessary packages and modules\nconst express = require('express');\nconst {\n readSingleProduct,\n readAllProducts,\n createProduct,\n updateProduct,\n deleteProduct,\n addToWhishlist,\n deleteProductFromWishlist\n} = require('../controllers/productController'); // Adjust path to your controller\nconst {\n param,\n body\n} = require('express-validator');\nconst verifyAccessToken = require('../middlewares/auth');\n\nconst router = express.Router();\n\n// Middleware for validation\nconst validateProduct = [\n body('name').isString().notEmpty(),\n body('description').isString(),\n body('price').isNumeric().notEmpty(),\n body('sizes').isArray(),\n body('colors').isArray(),\n body('images').isArray(),\n body('stock').isNumeric().notEmpty(),\n body('rating').isNumeric().optional()\n];\n\nconst validateProductId = [\n param('id').isMongoId().withMessage('Invalid product ID')\n];\n\n// Routes\nrouter.get('/products', verifyAccessToken, readAllProducts);\nrouter.get('/products/:id', verifyAccessToken, validateProductId, readSingleProduct);\nrouter.post('/products', verifyAccessToken, validateProduct, createProduct);\nrouter.put('/products/:id', verifyAccessToken, validateProduct, validateProductId, updateProduct);\nrouter.delete('/products', verifyAccessToken, deleteProduct);\nrouter.post('/addtowhish', verifyAccessToken, addToWhishlist);\nrouter.delete('/deletewhish', verifyAccessToken, deleteProductFromWishlist);\n\n// Additional routes for PUT, PATCH, and DELETE can be defined here\n\nmodule.exports = router;" } }