Advertisement
binm7md

Search.js

Dec 19th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. const express = require('express');
  2. const app = express();
  3. const port = 3000;
  4. const cors = require('cors');
  5. app.use(cors());
  6. // Sample book data
  7. const books = [
  8.     {
  9.         "book_title": "فن الامبالاه",
  10.         "book_genre": "اللغة العربية",
  11.         "book_thumbnail": "https://upload.wikimedia.org/wikipedia/ar/f/f0/%D8%BA%D9%84%D8%A7%D9%81_%D9%83%D8%AA%D8%A7%D8%A8_%D9%81%D9%86_%D8%A7%D9%84%D8%A7%D9%85%D8%A8%D8%A7%D9%84%D8%A7%D8%A9.jpg",
  12.         "row": "1",
  13.         "col": "1"
  14.     },
  15.     {
  16.         "book_title": "صحيح مسلم بشرح الننوي",
  17.         "book_genre": "العلوم الدينية",
  18.         "book_thumbnail": "https://www.ibnragb.com/wp-content/uploads/2021/03/2a42f97d-199d-4c95-840e-187e0c723789.jpg",
  19.         "row": "1",
  20.         "col": "1"
  21.     }
  22. ];
  23.  
  24. app.get('/search', (req, res) => {
  25.     const { book_title = '', book_genre = '' } = req.query;
  26.  
  27.     const results = books.filter(book => {
  28.         const titleMatch = book.book_title.includes(book_title);
  29.         const genreMatch = book_genre === '' || book.book_genre === book_genre;
  30.         return titleMatch && genreMatch;
  31.     });
  32.  
  33.     res.json(results);
  34. });
  35.  
  36. // Start the server
  37. app.listen(port, () => {
  38.     console.log(`Server is running on http://localhost:${port}`);
  39. });
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement