How to Implement Search in Express API Quickly and Easily
To implement search in an
Express API, use query parameters from the request URL to filter your data. Extract the search term with req.query, then filter your dataset accordingly before sending the response.Syntax
Use req.query to get search terms from the URL query string. Then filter your data based on this term before sending the response.
req.query: Object containing URL query parameters.- Filter function: Filters data matching the search term.
res.json(): Sends filtered data as JSON response.
javascript
app.get('/items', (req, res) => { const searchTerm = req.query.search || ''; const filteredItems = items.filter(item => item.name.toLowerCase().includes(searchTerm.toLowerCase())); res.json(filteredItems); });
Example
This example shows a simple Express API with an endpoint /items that returns items filtered by a search term passed as a query parameter search.
javascript
import express from 'express'; const app = express(); const port = 3000; const items = [ { id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }, { id: 3, name: 'Orange' }, { id: 4, name: 'Grape' }, { id: 5, name: 'Pineapple' } ]; app.get('/items', (req, res) => { const searchTerm = req.query.search || ''; const filteredItems = items.filter(item => item.name.toLowerCase().includes(searchTerm.toLowerCase()) ); res.json(filteredItems); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Output
Server running on http://localhost:3000
Example requests:
GET http://localhost:3000/items?search=ap
Response: [{"id":1,"name":"Apple"},{"id":5,"name":"Pineapple"}]
GET http://localhost:3000/items?search=an
Response: [{"id":2,"name":"Banana"},{"id":3,"name":"Orange"}]
Common Pitfalls
Common mistakes when implementing search in Express API include:
- Not handling case sensitivity, causing missed matches.
- Not providing a default value for the search term, leading to errors.
- Filtering on undefined or null data.
- Not validating or sanitizing input, which can cause security issues.
javascript
/* Wrong way: No default value and case-sensitive search */ app.get('/items', (req, res) => { const searchTerm = req.query.search; const filteredItems = items.filter(item => item.name.includes(searchTerm)); res.json(filteredItems); }); /* Right way: Default empty string and case-insensitive search */ app.get('/items', (req, res) => { const searchTerm = (req.query.search || '').toLowerCase(); const filteredItems = items.filter(item => item.name.toLowerCase().includes(searchTerm)); res.json(filteredItems); });
Quick Reference
- Use
req.queryto get search parameters. - Always provide a default value to avoid errors.
- Use
toLowerCase()for case-insensitive matching. - Filter your data array with
Array.filter(). - Send filtered results with
res.json().
Key Takeaways
Use
req.query to access search terms from the URL.Always handle case sensitivity by converting strings to lowercase.
Provide a default empty string for search terms to avoid errors.
Filter your data array using
Array.filter() with the search term.Return filtered results using
res.json() in your Express route.