A robust, in-memory autocomplete engine built with Node.js and Express. This project implements a custom Trie data structure capable of predicting string prefixes efficiently (modeled after thread-safe parsing algorithms previously developed in Go).
It guarantees millisecond response times by resolving active queries directly in memory and utilizing Redis for localized result caching, whilst persisting the origin dataset reliably inside a dynamic MongoDB instance.
- Backend Environment: Node.js
- API Framework: Express.js
- Database Layer: MongoDB (managed via Mongoose ORM)
- Caching Layer: Redis (managed via the
redisclient) - Core Data Structure: Custom Trie Structure (In-memory JavaScript algorithmic composition providing ultra-fast node routing and partial-suffix mapping)
- Live Documentation: Swagger UI (
swagger-ui-express)
- Bootstrapping: Upon startup, the Express application queries MongoDB for all existing
Itemschemas. The server transforms the database records and pushes them straight into the memory-bound Trie structure. - Search Lookups (
GET): When the API receives an autocomplete request via query parameter, it first checks Redis for an unexpired cached object. On a cache miss, the engine traces the query prefix through the Trie to extract ID keys, returning all known matching full strings. - Data Mutation (
POST/DELETE): Inserting or wiping database records synchronizes frictionlessly across both the MongoDB storage and the active memory pool without needing a reboot.
- Make sure you have Node.js natively installed.
- MongoDB must be running locally on the default port
27017 - (Optional) A Redis server running locally on default port
6379. If Redis isn’t bound to your environment, the code falls back natively to bypass caching gracefully without crashing.
- Start by installing the project dependencies:
cd autocomplete-node npm install - Boot the API. (We've rigged the project with
nodemonfor active live-reloading):npm run dev
(Alternatively for production use without reloading monitors: npm start)
Forget the terminal. We've equipped the server with an integrated interactive Swagger endpoint.
When npm run dev is running, pop open a browser and explore:
1. Create an Entry (POST /items)
curl -X POST http://localhost:3000/items \
-H "Content-Type: application/json" \
-d '{"itemId": 101, "value": "grey athletic shorts"}'2. Query Autocomplete Engine (GET /autocomplete)
Using the word 'grey', or partial matching.
curl "http://localhost:3000/autocomplete?q=grey"3. Delete an Entry (DELETE /items)
Targets the itemId reference provided previously.
curl -X DELETE http://localhost:3000/items/101