multy-stub/server/routers/old/my/index.js
Primakov Alexandr Alexandrovich 4b0d9b4dbc mongoose + tests
2024-10-16 11:06:23 +03:00

30 lines
956 B
JavaScript

const router = require('express').Router();
router.get('/catalog-data', (request, response) => {
response.send(require('./stubs/json/catalog-data/success.json'))
})
router.get('/item-data', (request, response) => {
response.send(require('./stubs/json/catalog-data/newsuccess.json'))
})
router.post('/search', (request, response) => {
const searchTerm = request.body.search.toLowerCase();
try {
const itemsData = require('./stubs/json/catalog-data/newsuccess.json');
const itemData = itemsData.data.find(item => item.name.toLowerCase().includes(searchTerm));
if (itemData) {
return response.json({ data: itemData });
} else {
console.log('Item not found');
return response.status(404).send('Item not found');
}
} catch (error) {
console.error('Error reading file:', error);
return response.status(500).send('Internal server error');
}
});
module.exports = router;