This is a simple Express.js application that implements basic user authentication using JSON Web Tokens (JWT). It includes features for user registration, login, and protected routes. The application uses MySQL as the database and bcrypt for password hashing.
- User Registration
- User Login
- Protected Routes with JWT Authorization
- Input Validation using
express-validator
- Basic Books CRUD
📂 express-crud
├── 📂 controllers
│ └── authController.js # Handles login, registration, and user info
│ └── booksController.js # Handles CRUD operations for books
├── 📂 middlewares
│ └── auth.js # JWT Authentication middleware
├── 📂 routes
│ └── auth.js # Auth-related routes (login, register, me)
│ └── books.js # Books-related routes (CRUD)
│ └── index.js # Main router that includes all routes
├── 📂 sql
│ └── express-jwt.sql # SQL file
├── 📂 utils
│ └── db.js # Database connection utility
│ └── jwt.js # JWT utility functions (sign, verify)
├── .env # Environment variables
├── .gitignore # Gitignore file
├── Express JWT Auth.postman_collection.json # Postman collection
├── index.js # Main Express application file
├── LICENSE # License file
├── package.json # Project dependencies and scripts
└── README.md # Project documentation (this file)
- Node.js (v14.x or later)
- MySQL Database
- Docker (optional)
- Postman (for API testing, optional)
-
Clone the repository:
git clone https://github.com/PH3487/express-crud.git
-
Navigate to the project directory:
cd express-crud
-
Install the dependencies:
npm install
-
Create a
.env
file in the root directory and add the following environment variables:PORT=3000 JWT_SECRET=your_jwt_secret_key DB_HOST=localhost DB_USER=root DB_PASSWORD=your_db_password DB_NAME=your_database_name
-
Set up the MySQL database:
- Create a MySQL database matching the name in
.env
. - Import sql file from sql/express-jwt.sql
- Run any necessary migrations or use your preferred database migration tool.
- Create a MySQL database matching the name in
To start the application in development mode:
npm run dev
The application will be running at: http://localhost:3000
.
A Postman collection is available for testing the API endpoints. You can import it by using the following steps:
- Import the
Express JWT Auth.postman_collection.json
file (located in the project root) into Postman. - Ensure that you set up the environment with the variable
HOST
set tohttp://localhost:3000
.
To run the tests using Jest and Supertest:
npm test
- Install Docker.
- Run
docker-compose up
to start the application. - Access the API via Express.js on port 3000.
- The database is powered by MySQL, and JWT is used for securing routes.
This project is licensed under the ISC License. See the LICENSE file for details.