0% found this document useful (0 votes)
2 views1 page

Query Parameters in Express API

Uploaded by

mehtashivam584
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Query Parameters in Express API

Uploaded by

mehtashivam584
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

const express = require('express');

const Dish = require('../models/Dish');


const { protect, admin } = require('../middleware/auth');
const router = [Link]();

// Get all dishes (with filters)


[Link]('/', async (req, res) => {
const { category, veg, price, search } = [Link];
let query = {};
if (category) [Link] = category;
if (veg !== undefined) [Link] = veg === 'true';
if (price) [Link] = { $lte: parseInt(price) };
if (search) [Link] = { $regex: search, $options: 'i' };

const dishes = await [Link](query).sort({ createdAt: -1 });


[Link]({ dishes, todaysSpecial: [Link](d => [Link]) });
});

// Add dish (admin only)


[Link]('/', protect, admin, async (req, res) => {
const dish = new Dish([Link]);
await [Link]();
[Link](dish);
});

// Update/Delete (admin)
[Link]('/:id', protect, admin, async (req, res) => {
const dish = await [Link]([Link], [Link], { new:

You might also like