Skip to content

Express Sequelize API repository! This project is a robust and scalable RESTful API built using Express.js and Sequelize. It provides a clean and efficient backend solution for managing data and handling various API requests in a Node.js environment.

License

Notifications You must be signed in to change notification settings

jsuyog2/express-sequelize-api

Repository files navigation

Express Sequelize API

Welcome to the Express Sequelize API repository! This project is a robust and scalable RESTful API built using Express.js and Sequelize. It provides a clean and efficient backend solution for managing data and handling various API requests in a Node.js environment.

Table of Contents

Features

  • RESTful API: Fully functional REST API endpoints for data management.
  • Sequelize Integration: Utilizes Sequelize ORM for database management with efficient querying.
  • JWT Authentication: Secure endpoints with JSON Web Token (JWT) based authentication.
  • Role-Based Access Control: Manage user permissions with role-based access.
  • Validation Middleware: Validate incoming requests using express-validator.
  • Logging Middleware: Request logging for monitoring and debugging.
  • Error Handling: Comprehensive error handling for robust API performance.
  • Unit Tests: Includes unit tests for middleware and controller functions to ensure code reliability.

Badges

Node.js CI

License

Coverage

Version

Maintenance

Tech Stack

  • Backend: Node.js, Express.js
  • Database: Sequelize (Supports various databases like PostgreSQL, MySQL, etc.)
  • Authentication: JWT
  • Testing: Jest
  • Validation: express-validator
  • Environment Management: dotenv

Installation

To get started with this project, follow these steps:

  1. Clone the Repository

    git clone https://github.com/jsuyog2/express-sequelize-api.git
    cd express-sequelize-api
  2. Install Dependencies

    Ensure you have Node.js and npm installed. Then run:

    npm install
  3. Set Up Environment Variables

Create a .env file in the root directory of the project. Configure the environment variables as described in Environment Variables.

Usage

Run Locally

Start the server

  npm run start

Build

To build the project using Webpack, use the following commands:

Production Build

To create an optimized production build:

  npm run build

This command will generate the bundled files optimized for production in the dist directory.

Start the Server

After building the project, you can start the server with:

  npm run prod

This command builds the project (if not already built) and then starts the server using the generated bundle.js file in the dist directory.

Environment Variables

To run this project, you need to configure the following environment variables in your .env file:

  • PRODUCTION: Set to true for production mode or false for development mode.
  • BASE_URL: The base URL for the application (e.g., http://127.0.0.1:3000).
  • PORT: The port on which the application will run (e.g., 3000).
  • CORS_LIST: Comma-separated list of allowed origins for CORS (e.g., http://localhost:4200).
  • JWT_SECRET: Secret key for signing JWT tokens.
  • JWT_PUBLICKEY_PATH: Path to the public key file for JWT verification.
  • JWT_PRIVATEKEY_PATH: Path to the private key file for JWT signing.
  • SESSION_SECRET: Secret key used for session management.
  • SEQUELIZE_CONNECT: Sequelize database connection string (e.g., postgres://postgres:1234@localhost/pgtest).
  • MAIL_HOST: SMTP server host for sending emails.
  • MAIL_PORT: SMTP server port for sending emails (e.g., 465).
  • MAIL_USERNAME: SMTP server username.
  • MAIL_PASSWORD: SMTP server password.

Here’s an example .env file with the required variables:

PRODUCTION=false
BASE_URL=http://127.0.0.1:3000
PORT=3000
CORS_LIST=http://localhost:4200

JWT_SECRET=api_secret_key
JWT_PUBLICKEY_PATH=./express-sequelize-api/key/public.key
JWT_PRIVATEKEY_PATH=./express-sequelize-api/key/private.key

SESSION_SECRET=api_session_secret

SEQUELIZE_CONNECT=postgres://postgres:1234@localhost/pgtest

MAIL_HOST=smtp.example.com
MAIL_PORT=465
MAIL_USERNAME=[email protected]
MAIL_PASSWORD=your_email_password

Ensure that you replace the placeholder values with your actual configuration details.

Generating Keys Using OpenSSL

Now you need to create Private and Public Key to generate a Token.

  1. Generate an RSA private key, of size 2048, and output it to a file named private.key:

    openssl genrsa -out key/private.key 2048
  2. Extract the public key from the key pair, which can be used in a certificate:

    openssl rsa -in key/private.key -outform PEM -pubout -out key/public.key

    Note: Make sure both key/private.key and key/public.key are saved in the key folder.

API Endpoints

This section provides an overview of the available API endpoints for the application. For detailed request and response formats, refer to the Postman Documentation.

Authentication

  • Login

    • Endpoint: POST /login
    • Description: Authenticates a user and returns a JWT token.
    • Body:
      {
        "username": "string",
        "password": "string"
      }
    • Responses:
      • 200 OK: Success
      • 401 Unauthorized: Invalid credentials
  • Signup

    • Endpoint: POST /signup
    • Description: Registers a new user.
    • Body:
      {
        "username": "string",
        "password": "string",
        "email": "string"
      }
    • Responses:
      • 201 Created: Success
      • 400 Bad Request: Validation errors
  • Logout

    • Endpoint: POST /logout
    • Description: Logs out the user and invalidates the session.
    • Responses:
      • 200 OK: Success

User Management

  • Get User Profile

    • Endpoint: GET /user
    • Description: Retrieves the authenticated user's profile.
    • Headers:
      • Authorization: Bearer token
    • Responses:
      • 200 OK: Success
      • 401 Unauthorized: Invalid token
  • Update User Profile

    • Endpoint: PUT /user
    • Description: Updates the authenticated user's profile information.
    • Headers:
      • Authorization: Bearer token
    • Body:
      {
        "username": "string",
        "email": "string"
      }
    • Responses:
      • 200 OK: Success
      • 400 Bad Request: Validation errors

For further details and examples, please refer to the Postman Collection.

Testing

To run the unit tests for the project, use:

npm test

This command will execute all the tests defined in the test directory.

Roadmap

The roadmap outlines planned features and improvements for the Express Sequelize API project. We aim to continuously enhance the project based on user feedback and evolving requirements.

Planned Features

  • Enhanced Security: Implement advanced security features like rate limiting and IP whitelisting.
  • User Roles and Permissions: Expand role-based access control with more granular permissions.
  • API Rate Limiting: Introduce rate limiting to prevent abuse and ensure fair usage.
  • Documentation Improvements: Enhance API documentation with more detailed examples and usage guidelines.
  • Performance Optimizations: Optimize query performance and server response times.
  • Internationalization (i18n): Add support for multiple languages to accommodate global users.
  • Additional Endpoints: Add new endpoints based on user needs and project requirements.

Future Improvements

  • Docker Support: Containerize the application for easier deployment and scaling.
  • GraphQL Integration: Explore integrating GraphQL for flexible data queries.
  • Automated Deployment: Set up CI/CD pipelines for automated testing and deployment.
  • Advanced Analytics: Incorporate analytics for monitoring API usage and performance metrics.

Support

If you encounter any issues or need assistance with the Express Sequelize API project, here are some ways you can get support:

  • Documentation: Check the Postman Collection and README for detailed information on API endpoints and usage.
  • FAQ: Review the Frequently Asked Questions (FAQ) section in the documentation for common queries and troubleshooting tips.
  • Community Support: Join discussions and ask questions on relevant forums or community channels.

We appreciate your feedback and are committed to providing timely support to ensure the best experience with the Express Sequelize API project.

Feedback

We welcome feedback on the Express Sequelize API project. If you have any suggestions, improvements, or issues, please let us know. You can provide feedback by:

  1. Opening an Issue: Report bugs, request features, or provide suggestions by creating a new issue in the Issues section of this repository.
  2. Submitting a Pull Request: If you have a solution or improvement, feel free to submit a pull request. Please ensure your changes adhere to the project's guidelines and standards.
  3. Contacting the Author: You can also directly reach out to the project maintainer, Suyog Dinesh Jadhav, with any feedback or questions.

Your input helps us improve the project and make it better for everyone!

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature).
  3. Make your changes and commit (git commit -am 'Add new feature').
  4. Push the branch (git push origin feature/your-feature).
  5. Create a new Pull Request.

FAQ

1. How do I set up the project locally?

To set up the project locally, follow these steps:

  1. Clone the Repository

    git clone https://github.com/jsuyog2/express-sequelize-api.git
    cd express-sequelize-api
  2. Install Dependencies Ensure you have Node.js and npm installed. Then run:

    npm install
  3. Set Up Environment Variables Create a .env file in the root directory and configure the necessary environment variables as described in the Environment Variables section.

  4. Start the Server

    npm start

2. How do I run the tests?

To run the unit tests, use:

npm test

This command will execute all tests defined in the test directory using Jest.

3. What should I do if I encounter a bug?

If you encounter a bug, follow these steps:

  1. Check Existing Issues Look at the Issues on GitHub to see if the problem has already been reported.

  2. Report a New Issue If your issue is not listed, create a New Issue on GitHub. Provide a detailed description of the issue, including steps to reproduce it and any relevant screenshots or error logs.

4. How can I contribute to the project?

Contributions are welcome! To contribute:

  1. Fork the Repository

    git clone https://github.com/your-username/express-sequelize-api.git
  2. Create a New Branch

    git checkout -b feature/your-feature
  3. Make Your Changes Make the necessary changes and commit them:

    git commit -am 'Add new feature'
  4. Push Your Branch

    git push origin feature/your-feature
  5. Create a Pull Request Open a Pull Request on GitHub with a description of your changes.

5. Where can I find the API documentation?

API documentation, including endpoint details and usage examples, can be found in the Postman Documentation.

6. How do I configure environment variables?

Refer to the Environment Variables section for details on configuring environment variables in your .env file.

7. Can I use this project in production?

Yes, but make sure to set the PRODUCTION environment variable to true and properly configure your production environment settings in the .env file.

If you have any other questions or need further assistance, feel free to reach out via email.

Acknowledgements

  • Express.js: A fast, unopinionated, minimalist web framework for Node.js.
  • Sequelize: A promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server.
  • jsonwebtoken: A library for signing and verifying JSON Web Tokens.
  • bcryptjs: A library to hash and compare passwords securely.
  • express-validator: A set of express.js middlewares for validation.
  • dotenv: A module to load environment variables from a .env file into process.env.
  • Jest: A delightful JavaScript testing framework with a focus on simplicity.

Author

This project is maintained by Suyog Dinesh Jadhav.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Express Sequelize API repository! This project is a robust and scalable RESTful API built using Express.js and Sequelize. It provides a clean and efficient backend solution for managing data and handling various API requests in a Node.js environment.

Topics

Resources

License

Stars

Watchers

Forks