This project is a backend REST API designed to handle keyword searches on an external API and store the results in a database. It was created as part of the ShareTrip Node.js developer assessment test.
- Introduction
- Technologies
- Setup
- Usage
- Database Schema
- Example
- Swagger Documentation
- Testing
- Conclusion
The ShareTrip Backend API is a Node.js application built to handle user requests for keyword searches. Upon receiving a request, the API fetches data from an external API, performs keyword matching on the response, and stores the matching records in a database. The API then returns the search results to the user.
/api/search?keyword=<user_keyword>
- User searches for mango:
localhost:3000/api/search?keyword=mango
- Node.js
- Express.js
- MySQL (database of choice)
- Sequelize (A promise-based Node.js ORM)
- Winston (for logging)
- Swagger (for API documentation)
- Docker (for containerization)
- Docker Compose(running multi-container Docker applications)
- Jest (test runner)
- Artillery (test runner)
- Clone the repository:
git clone https://github.com/JsIqbal/info-hub.git
- Set up the environment variables:
- Know this: Environment variables values differ as per the configuration of the software.
PORT=3000
DB_NAME=<database_name>
DB_HOST=<database_host>
DB_USER=<database_user>
DB_PASSWORD=<can be "" or password>
EXTERNAL_API=<your_external_api>
- Run in Terminal:
make serve
- you will see a choosing option.
- Either you can select 1 to run the project using docker or you can choose 2 to locally run the project. Instructions are given on the terminal.
- Make a GET request to the search endpoint:
http://localhost:3000/search?keyword=ratione
- example response if searh finds any contents:
[
{
"userId": 2,
"id": 17,
"title": "fugit voluptas sed molestias voluptatem provident",
"body": "eos voluptas et aut odit natus earum\naspernatur fuga molestiae ullam\ndeserunt ratione qui eos\nqui nihil ratione nemo velit ut aut id quo"
}
]
- The database schema is designed to capture user searches and matching blog posts. The basic schema includes fields such as timestamp, user ID, and references between tables for a one-to-many relationship.
- Suppose a user searches for "mango," and the external API returns the following posts:
[
{
"userId": 10,
"id": 96,
"title": "Orange is best",
"body": "Orange is of orange color"
},
{
"userId": 10,
"id": 97,
"title": "Best fruit",
"body": "Eat mango, mangoes are being sold on the local market"
},
{
"userId": 10,
"id": 98,
"title": "Mango juice",
"body": "Don’t like fruits? Try juice instead"
}
]
- Matching posts with the keyword "mango" (id 97, 98) are stored in the database, and the API returns these blog posts as the response.
- Swagger documentation for the API is available at:
http://localhost:3000/api-docs
Explore the API endpoints and test requests using Swagger.
-
The ShareTrip Backend API comes with a robust testing suite to ensure the reliability and correctness of its functionality.
- The tests covers:
- Integration test
- Unit test
- Artillery test
- The tests covers:
npm test
- This command will trigger the Jest test runner, executing basic tests to ensure that the application starts without errors. It's a quick way to verify the initial configuration of the project.
npm run test:load
- This command will trigger the Artillery test runner, executing basic load tests to ensure that the application can handle concurrent users and traffic efficiently.
The ShareTrip Backend API is a robust Node.js application, efficiently managing keyword searches and database operations. Leveraging technologies like Express.js, Sequelize, and Docker, it ensures scalability and reliability. The well-documented README provides clear setup instructions, Swagger documentation, and comprehensive testing with Jest and Artillery.