This Node.js backend service leverages Express.js for routing and Mongoose for MongoDB object modeling, providing a scalable API to analyze and manage string data with computed properties. It efficiently processes strings, stores their characteristics, and offers flexible retrieval options, including sophisticated filtering and natural language querying capabilities.
- Node.js, Express.js: Built on a robust and asynchronous JavaScript runtime and a minimalist web framework for efficient API development.
- Mongoose, MongoDB: Utilizes Mongoose for elegant object data modeling, enabling seamless interaction with a MongoDB NoSQL database for persistent storage and querying of string analysis results.
- Comprehensive String Analysis: Automatically calculates various string properties including length, palindrome status, unique character count, word count, SHA256 hash, and a detailed character frequency map.
- Advanced Filtering: Supports querying strings based on their computed properties such as palindrome status, specified length ranges, exact word counts, and the presence of particular characters.
- Natural Language Querying: Interprets human-readable queries to filter strings, allowing for intuitive data retrieval, significantly enhancing user experience.
- RESTful API Design: Implements standardized HTTP methods and predictable URLs for managing string resources, ensuring a clean and consistent API interface.
- Centralized Error Handling: Incorporates middleware for graceful error management and clear 404 responses, improving API reliability and user feedback.
To set up the String Analyzer Service API locally, follow these steps:
First, clone the repository and install the dependencies:
π Clone the Repository:
git clone https://github.com/dprof-code/string-analyzer-service.git
cd string-analyzer-serviceπ¦ Install Dependencies:
npm install
# or
yarn installCreate a .env file in the root directory of the project and populate it with the following required variables:
PORT: The port number on which the Express server will run.- Example:
PORT=5000
- Example:
DB_URI: The MongoDB connection URI.- Example:
DB_URI=mongodb+srv://<username>:<password>@string-analyzer-cluster.pws0m6l.mongodb.net/?retryWrites=true&w=majority&appName=string-analyzer-cluster
- Example:
NODE_ENV: The environment mode (e.g.,development,production). This affects error stack trace visibility.- Example:
NODE_ENV=development
- Example:
Once the project is installed and environment variables are configured, you can start the development server:
π Start the Development Server:
npm run devThe API will be running on http://localhost:<PORT> (e.g., http://localhost:5000). You can interact with the endpoints using tools like Postman, Insomnia, or cURL.
Example using curl to create a string:
curl -X POST -H "Content-Type: application/json" -d '{"value": "hello world"}' http://localhost:5000/stringsExample using curl to get strings that are palindromes and longer than 3 characters:
curl http://localhost:5000/strings?is_palindrome=true&min_length=3http://localhost:<PORT>/strings (or http://localhost:5000/strings with example PORT)
Overview: Analyzes a new string value and stores its computed properties in the database. Request:
{
"value": "example string for analysis"
}Response:
{
"_id": "65e6e7c1c7b8e1f0a3c7b8e1",
"id": "a9b3d1c4e7f0a2b6c8d0e9f1a3b5c7d9e0f2a4b8",
"value": "example string for analysis",
"properties": {
"length": 27,
"is_palindrome": false,
"unique_characters": 13,
"word_count": 5,
"sha256_hash": "a9b3d1c4e7f0a2b6c8d0e9f1a3b5c7d9e0f2a4b8c1d5e0f9a7b2c6d4e1f8a3b7",
"character_frequency_map": {
"e": 3,
"x": 1,
"a": 4,
"m": 1,
"p": 1,
"l": 1,
"s": 3,
"t": 1,
"r": 2,
"i": 2,
"n": 2,
"g": 2,
"f": 1,
"o": 1
}
},
"created_at": "2024-03-05T10:00:00.000Z",
"__v": 0
}Errors:
400 Bad Request: Invalid request body or missing "value" field.{ "message": "Invalid request body or missing \"value\" field" }409 Conflict: String already exists in the system.{ "message": "String already exists in the system" }422 Unprocessable Entity: Invalid data type for "value" (must be string).{ "message": "Invalid request body or missing data type for \"value\" (must be string)" }500 Internal Server Error: Generic server error.
Overview: Retrieves a list of analyzed strings, with optional filtering capabilities based on string properties. Query Parameters:
is_palindrome:trueorfalse(boolean string to filter for palindromic status)min_length:number(minimum length inclusive)max_length:number(maximum length inclusive)word_count:number(exact word count)contains_character:string(a single character to check for its presence in the string) Example Request:GET /strings?is_palindrome=true&min_length=5Response:
{
"data": [
{
"id": "65e6e7c1c7b8e1f0a3c7b8e2",
"value": "madam",
"properties": {
"length": 5,
"is_palindrome": true,
"unique_characters": 3,
"word_count": 1,
"sha256_hash": "...",
"character_frequency_map": { "m": 2, "a": 2, "d": 1 }
},
"created_at": "2024-03-05T10:01:00.000Z"
}
],
"count": 1,
"filters_applied": {
"is_palindrome": true,
"min_length": 5
}
}Errors:
400 Bad Request: Invalid query parameter detected.{ "message": "Invalid query parameter detected: invalid_param." }500 Internal Server Error: Generic server error.
Overview: Filters strings based on a natural language query, interpreting human-readable phrases into database filters. Query Parameters:
query:string(e.g., "find me palindromic strings longer than 5 containing the letter a") Example Request:GET /strings/filter-by-natural-language?query=find%20me%20palindromic%20strings%20longer%20than%205%20containing%20the%20letter%20aResponse:
{
"data": [
"madam",
"level",
"refer"
],
"count": 3,
"interpreted_query": {
"original": "find me palindromic strings longer than 5 containing the letter a",
"parsed_filters": {
"is_palindrome": true,
"min_length": 6,
"contains_character": "a"
}
}
}Errors:
400 Bad Request: Unable to parse natural language query.{ "message": "Unable to parse natural language query" }500 Internal Server Error: Generic server error.
Overview: Retrieves the full analysis details for a specific string value. Path Parameters:
string:string(The actual string value to retrieve, e.g., "hello") Example Request:GET /strings/helloResponse:
{
"id": "a9b3d1c4e7f0a2b6c8d0e9f1a3b5c7d9e0f2a4b8",
"value": "hello",
"properties": {
"length": 5,
"is_palindrome": false,
"unique_characters": 4,
"word_count": 1,
"sha256_hash": "a9b3d1c4e7f0a2b6c8d0e9f1a3b5c7d9e0f2a4b8c1d5e0f9a7b2c6d4e1f8a3b7",
"character_frequency_map": { "h": 1, "e": 1, "l": 2, "o": 1 }
},
"created_at": "2024-03-05T10:00:00.000Z"
}Errors:
404 Not Found: String does not exist in the system.{ "message": "String does not exist in the system" }500 Internal Server Error: Generic server error.
Overview: Deletes a specific string and its analysis from the database. Path Parameters:
string:string(The actual string value to delete, e.g., "world") Example Request:DELETE /strings/worldResponse:204 No Content(Empty body on successful deletion) Errors:404 Not Found: String does not exist in the system.{ "message": "String does not exist in the system" }500 Internal Server Error: Generic server error.
| Technology | Description |
|---|---|
| Node.js | A powerful JavaScript runtime for server-side development. |
| Express.js | A fast, unopinionated, minimalist web framework for Node.js. |
| Mongoose | An elegant MongoDB object data modeling (ODM) library for Node.js. |
| MongoDB | A leading NoSQL document database known for scalability and flexibility. |
| Dotenv | A zero-dependency module that loads environment variables from a .env file. |
We welcome contributions to enhance the String Analyzer Service! If you're interested in improving this project, please follow these guidelines:
β¨ Fork the repository to your GitHub account.
πΏ Create a new branch for your feature or bug fix (e.g., git checkout -b feature/add-new-analysis).
π οΈ Make your changes, ensuring code quality and functionality. (While no dedicated test script is provided, consider adding tests for new features.)
π¬ Commit your changes with a clear and descriptive message (e.g., git commit -m 'feat: Implement new string property calculation').
β¬οΈ Push your changes to your forked repository (git push origin feature/add-new-analysis).
π Open a pull request against the main branch of this repository, detailing your changes.
This project is licensed under the ISC License. For more details, refer to the license field in the package.json file.
Developed by a passionate backend developer dedicated to building robust and efficient services.