Welcome to the Lema Test Project, a full-stack application designed to manage users and their posts. This project consists of a Frontend built with Next.js and React, and a Backend powered by Express.js and Sequelize. The application includes robust API endpoints for managing users and posts, complete with pagination and comprehensive Postman API documentation.
- Project Overview
- Prerequisites
- Project Structure
- Installation
- Configuration
- Running the Application
- API Documentation
- Testing
- Deployment
- Contributing
- License
- Troubleshooting
- Contact
The Lema Test Project is a full-stack web application that allows users to:
- Manage Users: Create, read, update, and delete user profiles.
- Manage Posts: Each user can have multiple posts. Users can create, view, and delete their posts.
- Pagination: Efficiently handle large datasets with pagination on both users and posts.
- API Integration: Seamless communication between the frontend and backend through RESTful APIs.
- Testing: Comprehensive testing for both frontend and backend to ensure reliability.
Before setting up the project, ensure you have the following installed on your machine:
- Node.js: Version 16.x.x or higher
- npm: Version 8.x.x or higher (comes with Node.js)
- Git: For version control
- Postman: For API testing (optional but recommended)
Note: It's recommended to use a version manager like nvm to manage multiple Node.js versions.
lema-test/
├── backend/
│ ├── node_modules/
│ ├── tests/
│ ├── server.js
│ ├── package.json
│ ├── .env
│ └── ...other backend files
├── frontend/
│ ├── node_modules/
│ ├── src/
│ │ ├── app/
│ │ │ ├── favicon.ico
│ │ │ └── ...other frontend files
│ ├── public/
│ ├── package.json
│ ├── tailwind.config.js
│ ├── postcss.config.js
│ └── ...other frontend files
├── README.md
└── ...other root files
- backend/: Contains the Express.js server, API routes, and related configurations.
- frontend/: Contains the Next.js (react) application with React components and related configurations.
First, clone the repository to your local machine:
git clone https://github.com/desmondezo1/assessment_test_lema.git
cd lema-test
-
Navigate to the Backend Directory:
cd backend
-
Install Dependencies:
npm install
-
Configure Environment Variables:
Create a
.env
file in thebackend
directory based on the.env.example
(if available). If not provided, you can create one with the necessary configurations.touch .env
Example
.env
Configuration:PORT=3000 JWT_SECRET=your_jwt_secret
-
Run Database Migrations (If Applicable):
If using Sequelize with migrations, ensure to run them. This step may vary based on your project setup.
npx sequelize-cli db:migrate
-
Navigate to the Frontend Directory:
Open a new terminal window/tab and navigate to the
frontend
directory:cd frontend
-
Install Dependencies:
npm install
-
Configure Environment Variables:
Create a
.env.local
file in thefrontend
directory for frontend-specific environment variables.touch .env.local
Example
.env.local
Configuration:NEXT_PUBLIC_API_URL=http://localhost:3000/api/v1
Note: Adjust the
NEXT_PUBLIC_API_URL
based on your backend server's URL and port.
Ensure the following environment variables are set in the backend/.env
file:
- PORT: The port on which the backend server will run (default:
3001
). - JWT_SECRET: A secret key for JWT authentication.
- DATABASE_URL: Connection string for your database (if applicable).
- DATABASE_URL: Connection string for your database (if applicable).
- FRONTEND_URL: http://localhost:3000
Example .env
File:
PORT=3000
JWT_SECRET=your_jwt_secret
DATABASE_URL=sqlite://./database.sqlite
Note: Adjust the
DATABASE_URL
based on your database configuration. The example uses SQLite.
-
Navigate to Backend Directory:
cd backend
-
Start the Server in Development Mode:
npm run dev
This command uses nodemon to watch for file changes and automatically restarts the server.
-
Verify the Server is Running:
Open your browser and navigate to
http://localhost:3000
(or the port specified in your.env
file).
-
Navigate to Frontend Directory:
Open a new terminal window/tab and navigate to the
frontend
directory:cd frontend
-
Start the Development Server:
npm run dev
This command starts the Next.js development server.
-
Access the Frontend:
Open your browser and navigate to
http://localhost:3000
(or the port specified in your frontend configuration).
This project provides a set of RESTful APIs for managing users and their posts. Below are the available endpoints along with instructions on how to import and use them in Postman.
To test the APIs, you can use the provided Postman collection.
-
Install Postman:
If you haven't already, download and install Postman.
-
Import the Collection:
- Open Postman.
- Click on the
Import
button. - Select the
Raw Text
tab. - Paste the provided Postman collection JSON (from your message) into the text area.
- Click
Continue
, thenImport
.
-
Access the Imported Collection:
After importing, the collection named
Lema_test
will appear in your Postman workspace. You can now interact with the API endpoints.
-
Endpoint:
GET /api/v1/users
-
Description: Retrieves a list of users with pagination.
-
URL:
http://localhost:3000/api/v1/users
-
Response:
{ "success": true, "data": [ { "id": "d3a4ec91a50447ebb64e395e124caf40", "name": "Prof. Benedict Prosacco", "username": "wEZBVcG", "email": "[email protected]", "phone": "641-107-9352", "address": { "id": "c5358841705b44178fc464f51c69e24e", "userId": "d3a4ec91a50447ebb64e395e124caf40", "street": "4709 Blagden Terrace Northwest", "state": "DC", "city": "Washington", "zipcode": "20011" } }, // ... more users ], "pagination": { "total": 100, "currentPage": 1, "totalPages": 25, "limit": 4 } }
-
Endpoint:
GET /api/v1/users/:userId/posts
-
Description: Retrieves posts made by a specific user.
-
URL:
http://localhost:3001/api/v1/users/d3a4ec91a50447ebb64e395e124caf40/posts
-
Response:
{ "success": true, "data": [ { "id": "5da145706f1a40d581fdcd4bc92f73a9", "userId": "d3a4ec91a50447ebb64e395e124caf40", "title": "Et occaecati ab cupiditate cum exercitationem.", "body": "Nobis est nulla et nesciunt odio...", "createdAt": "2024-11-06T13:48:58+02:00", "user_id": "d3a4ec91a50447ebb64e395e124caf40" }, // ... more posts ], "pagination": { "total": 4, "currentPage": 1, "totalPages": 1, "limit": 4 } }
-
Endpoint:
DELETE /api/v1/posts/:postId
-
Description: Deletes a specific post.
-
URL:
http://localhost:3001/api/v1/posts/5da145706f1a40d581fdcd4bc92f73a9
-
Response:
{ "success": true, "message": "Post deleted successfully", "data": { "message": "Post deleted successfully" } }
-
Endpoint:
GET /api/v1/posts
-
Description: Retrieves all posts with pagination.
-
URL:
http://localhost:3001/api/v1/posts
-
Response:
{ "success": true, "data": [ { "id": "a13e7750c915422480b2c65925c29449", "userId": "d3a4ec91a50447ebb64e395e124caf40", "title": "Consequuntur placeat porro quos excepturi qui.", "body": "Explicabo ducimus molestias voluptas et tempore...", "createdAt": "2023-12-05T10:39:54+02:00", "user_id": "d3a4ec91a50447ebb64e395e124caf40", "user": { "name": "Prof. Benedict Prosacco" } }, // ... more posts ], "pagination": { "total": 849, "currentPage": 1, "totalPages": 213, "limit": 4 } }
Note: Replace
localhost
and port3000
with your server's actual host and port if different.
Testing ensures the reliability and correctness of your application. Both the frontend and backend include tests that can be run using Jest.
-
Navigate to Backend Directory:
cd backend
-
Run Tests:
npm test
-
Watch Mode:
npm run test:watch
-
-
Test Coverage:
To generate a test coverage report:
npx jest --coverage
-
Navigate to Frontend Directory:
cd frontend
-
Run Tests:
npm test
-
Watch Mode:
npm run test:watch
-
-
Test Coverage:
To generate a test coverage report:
npx jest --coverage
Note: Ensure that both frontend and backend servers are not running while executing tests to avoid port conflicts.
Deploying your application allows users to access it over the internet. Below are general guidelines for deploying both the frontend and backend.
-
Choose a Hosting Provider:
Popular choices include:
- Heroku
- AWS (Elastic Beanstalk, EC2)
- DigitalOcean
- Vercel (supports Node.js)
-
Setup Environment Variables:
Ensure all necessary environment variables are set on the hosting platform.
-
Database Configuration:
If using a production database (e.g., PostgreSQL, MySQL), ensure it's properly configured and connected.
-
Deploy:
Follow the hosting provider's instructions to deploy your Express.js application.
-
Choose a Hosting Provider:
Popular choices include:
- Vercel (optimized for Next.js)
- Netlify
- AWS (S3 + CloudFront)
- Heroku
-
Configure Environment Variables:
Ensure all necessary environment variables are set, especially
NEXT_PUBLIC_API_URL
. -
Build the Application:
npm run build
-
Deploy:
Follow the hosting provider's instructions to deploy your Next.js application.
Contributions are welcome! To contribute to this project, follow these steps:
-
Fork the Repository:
Click the
Fork
button on the repository page to create your own copy. -
Clone Your Fork:
git clone https://github.com/desmondezo1/assessment_test_lema.git cd lema-test
-
Create a Branch:
git checkout -b feature/YourFeatureName
-
Make Changes:
Implement your feature or fix.
-
Commit Changes:
git commit -m "Add feature: YourFeatureName"
-
Push to Your Fork:
git push origin feature/YourFeatureName
-
Create a Pull Request:
Go to the original repository and create a pull request from your fork.
This project is licensed under the MIT License.
Encountering issues while setting up or running the project? Here are some common problems and solutions:
- Issue: Another application is using the same port.
- Solution: Change the port in your
.env
file or stop the application using the conflicting port.
- Issue: The application fails to start due to missing variables.
- Solution: Ensure all required environment variables are set in the
.env
files for both frontend and backend.
- Issue: Unable to connect to the database.
- Solution: Verify your
DATABASE_URL
and ensure the database server is running.
-
Issue: Errors during the build process.
-
Solution:
- Check for syntax errors in your code.
- Ensure all dependencies are installed.
- Clear caches by deleting
node_modules
andpackage-lock.json
, then reinstalling.
rm -rf node_modules package-lock.json npm install
- Issue: Frontend cannot communicate with the backend.
- Solution:
- Ensure the backend server is running.
- Verify the
NEXT_PUBLIC_API_URL
in the frontend.env.local
file points to the correct backend URL. - Check for CORS issues in the backend configuration.
Based on your earlier error logs, you might encounter issues related to the favicon.ico
file being too large or containing invalid characters.
- Solution:
- File Size: Ensure
favicon.ico
is under 8MB. Preferably, it should be around 100KB. - File Path: Avoid special characters and spaces in the file path. Rename directories if necessary to remove apostrophes or other special characters.
- File Size: Ensure
If you encounter compatibility issues with Next.js versions:
-
Solution:
- Ensure you're using a stable version of Next.js. As of the latest update, Next.js v15.0.3 is not officially supported.
- Downgrade to the latest stable version (e.g., v13.x.x).
npm install next@latest
If you encounter a bus error
in your terminal:
- Solution:
- Restart your terminal.
- Ensure no conflicting processes are running.
- Reinstall dependencies.
- Check for corrupted files.
For any questions, issues, or suggestions, please open an issue in the GitHub repository or contact the project maintainer at [email protected].
Thank you for using the Lema Test Project! We hope this guide helps you set up and run the application smoothly. Happy coding!