This project implements a Node.js microservices architecture using Express for CRUD operations. The data is stored in MongoDB, and the application includes JWT-based authentication. The project also integrates with Redis for caching.
- Prerequisites
- Project Setup
- Environment Variables
- Sample User Credentials
- Running the Application
- API Endpoints
- JWT Authentication
- Caching with Redis
- Unit Testing
- Postman Collection
Before you begin, ensure you have the following installed on your system:
- Node.js: v20
- npm: v10
- MongoDB: Installed and running locally or on a remote server
- Redis: Installed and running locally or on a remote server
- Postman: Installed for API testing
git https://github.com/Fadhilamadan/ms-fadhilamadan-betest.git
cd ms-fadhilamadan-betest
npm install
Create a .env
file in the root directory of the project and configure the following environment variables:
# Application configuration
PORT=3000
# JWT configuration
JWT_SECRET=your_jwt_secret_key
JWT_DURATION=your_jwt_duration
# Redis configuration
REDIS_PORT=localhost
REDIS_HOST=6379
REDIS_PASSWORD=password
# MongoDB configuration
MONGO_URI=mongodb://localhost:27017/your_database_name
Ensure that they are correctly set according to your environment.
For convenience, the following sample user credentials are provided. You can use these to log in and test the application's authentication and user management features.
- Username:
fadhilamadan
- Password:
password
These credentials are pre-populated in the database. You can use them directly to generate a JWT token via the login API.
To start the application in development mode:
npm run dev
This command will start the application using nodemon
, a tool that automatically reloads the server when code changes are detected. If you don't already have nodemon
installed globally, you can install it by running npm install -g nodemon
.
The application will be available at http://localhost:3000/api
. You can use Postman or any other API client to interact with the endpoints.
- POST
/api/account/login
- Request Body:
{ "userName": "fadhilamadan", "password": "password" }
- Response:
{ "token": "JWT Token" }
- Request Body:
-
GET
/api/user/accountNumber/:accountNumber
- Request Header:
Authorization: Bearer <JWT Token>
- Response:
{ "userId": "string", "fullName": "string", "accountNumber": "string", "emailAddress": "string", "registrationNumber": "string" }
- Request Header:
-
GET
/api/user/registrationNumber/:registrationNumber
- Request Header:
Authorization: Bearer <JWT Token>
- Response:
{ "userId": "string", "fullName": "string", "accountNumber": "string", "emailAddress": "string", "registrationNumber": "string" }
- Request Header:
- API endpoints are protected using JWT authentication. You must include a valid JWT token in the
Authorization
header of your requests.
To generate a JWT token, send a POST
request to the /api/account/login
endpoint with the user’s credentials. The server will respond with a JWT token if the credentials are valid.
Include the JWT token in the Authorization
header of your requests:
Authorization: Bearer <JWT Token>
The application caches user information in Redis. When fetching user data by accountNumber
or registrationNumber
, the application first checks the Redis cache before querying MongoDB.
Ensure Redis is running and configured correctly in the .env
file.
The project includes unit tests to verify the functionality of the application. You can run the tests using:
npm run test
This command will execute all the tests using Jest.
To test the API endpoints:
- Access the Postman Collection: Click here to open the collection.
- Set Environment: Set the
url
to your API (e.g.,https://ms-fadhilamadan-betest.vercel.app/api
) and add yourtoken
after logging in. - Send Requests: Use the predefined requests in the collection to interact with the API.