This project template facilitates the development of web applications using Express.js and MySQL. It includes user authentication via cookies and provides a foundation for building APIs to manage recipes or similar entities.
This template is designed to help developers create a robust web application backend using Express.js and MySQL. It focuses on user registration, authentication, and authorization, allowing authenticated users to manage recipes (or similar entities) through defined APIs.
- Implement various APIs using Express.js (e.g., Recipes).
- Support user registration and login functionality.
- Non-authenticated users can view all recipes but cannot modify them.
- Authenticated users can perform CRUD (Create, Read, Update, Delete) operations on recipes.
Project/
|-- config/
| ├── db.js # Database configuration
|-- controllers/
| ├── userController.js # Handles user-related operations
|
|-- middleware/
| ├── verifyToken.js # Middleware to verify user authentication
|-- models/
| ├── user.js # Defines user schema for MySQL
|-- routes/
| ├── userRoutes.js # Routes for user-related endpoints
|
|-- utils/
| ├── hashPassword.js # Utility to hash user passwords
| ├── matchPasswords.js # Utility to compare passwords
| ├── validateEmail.js # Utility to validate email format
| ├── validatePasswords.js # Utility to validate password complexity
|-- .babelrc # Babel configuration for ES6 support
|-- .env # Environment variables configuration
|-- index.js # Entry point of the application
|-- package.json # Dependencies and scripts
|-- README.md # This file
-
Use this Template Repo: Use this template to create your repo
-
Install dependencies:
npm install
-
Configure environment variables:
-
Create a
.env
file in the root directory and add the following:DB_HOST=your_database_host DB_USER=your_database_user DB_PASSWORD=your_database_password DB_NAME=your_database_name SECRET_KEY=your_secret_key
-
-
Create a database:
CREATE DATABASE your_database_name;
-
Run the application:
npm run dev
Ensure the following environment variables are set in your .env
file:
PORT=5002
TOKEN_ACCESS_SECRET=your_token_secret
DB_NAME=your_database_name
DB_USER=your_database_user
DB_PASSWORD=your_database_password
DB_HOST=your_database_host
-
POST /register
- Registers a new user.
-
POST /login
- Logs in an existing user.
-
POST /logout
- Logout user.
-
GET /recipes
- Retrieves all recipes.
-
POST /recipes
- Creates a new recipe (authenticated users only).
-
GET /recipes/:id
- Retrieves a single recipe by ID.
-
PUT /recipes/:id
- Updates a recipe by ID (authenticated users only).
-
DELETE /recipes/:id
- Deletes a recipe by ID (authenticated users only).
Handles user registration, login, and other user-related actions.
Manages CRUD operations for recipes.
Middleware function to verify user tokens for authentication purposes.
Utility to hash user passwords for secure storage.
Utility to compare password and confirmPassword.
Utility to validate email format.
Utility to ensure passwords meet required complexity criteria.
- Users must register and log in to perform certain actions.
- Authentication is handled using cookies.
- The
verifyToken
middleware function ensures that only authenticated users can access restricted routes.