A simple Node.js + Express backend that provides autocomplete search functionality on a list of products stored in a local JSON file (seeded from DummyJSON API).
- 🔍
/products/searchendpoint with query support, pagination, and relevance scoring - 🧠 Search ranking based on:
- Title starts with query → score 3
- Title includes query → score 2
- Brand includes query → score 1
- 🛠 Uses a local JSON file as a lightweight data store (no external DB)
- 📄 Written in TypeScript for type safety
- ✅ Includes Jest test coverage for core functionality
- 📦 Auto-fetches & seeds 100+ products on first run, skips if data exists
# 1. Clone the repo
git clone https://github.com/Shubh6665/product-autocomplete-backend.git
cd product-autocomplete-backend
# 2. Install dependencies
npm install
# 3. Start the development server
npm startThe server will start on: http://localhost:3000
📝 On first run, the app automatically fetches products from DummyJSON and saves them to
data/products.json.
- Uses data/products.json to simulate a database.
- Matches queries against
titleandbrand, ranking results by relevance - Pagination support is built-in using
limitandskipquery parameters. - Input validation ensures only meaningful queries (≥2 characters) are processed.
Request:
GET /products/search?q=red&limit=5&skip=0Curl example:
curl "http://localhost:3000/products/search?q=red&limit=5&skip=0"Sample response:
{
"count": 5,
"results": [
{
"id": 4,
"title": "Red Lipstick",
"brand": "Chic Cosmetics",
"category": "beauty",
"price": 12.99,
"score": 3
},
...
]
}- Query parameter
qis required and must be at least 2 characters long. - Returns HTTP
400 Bad Requestif validation fails. - Returns an empty result array if no matches are found.
To run the existing Jest tests, simply execute:
npm testTests cover:
- Valid search queries
- Case-insensitivity
- Pagination logic (limit and skip)
- Handling of invalid or too-short queries
- No-match scenarios
Run npm test -- --coverage for coverage report.
Sample output:
--------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------|---------|----------|---------|---------|-------------------
All files | 95.23 | 78.57 | 100 | 100 |
productServices.ts | 95.23 | 78.57 | 100 | 100 | 8,16,22
--------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 0.708 s, estimated 1 s
product-autocomplete-backend/
│
├── src/
│ ├── controllers/
│ │ └── productController.ts
│ ├── data/
│ │ └── products.json
│ ├── services/
│ │ └── productServices.ts
│ ├── routes/
│ │ └── productRoutes.ts
│ ├── utils/
│ │ └── fetchandseed.ts
│ └── index.ts
│
├── tests/
│ └── productServices.test.ts
│
├── package.json
├── tsconfig.json
├── jest.config.js
└── README.md