This is a Node.js RESTful API project using Sequelize as the ORM, focused on creating and managing users, posts, and categories for a blog. The project was developed as part of an academic challenge, with the goal of applying concepts such as database modeling, table relationships, full CRUD operations, authentication validation, and development best practices using Express.js and Sequelize.
- Node.js
- Express.js
- Sequelize
- MySQL
- JSON Web Token (JWT)
- Docker
- ESLint + Airbnb Style Guide
src/models/: Sequelize models using functional syntaxsrc/migrations/: Migrations to generate database tablessrc/controllers/: Endpoint logicsrc/services/: Business logicsrc/middlewares/: Reusable middlewares including authenticationsrc/routes/: API routes and endpoints
- Presence and format validation for email, password, and user name
- JWT token verification for protected endpoints
- Permission restrictions: only the post author can edit or delete their own posts
- Standardized error messages according to project specifications
- Using middlewares to centralize JWT authentication
- Creating Sequelize associations (
1:N,N:N) withbelongsTo,hasMany, andbelongsToMany - Implementing search queries using
Sequelize.Op.like - Using transactions to ensure integrity in many-to-many relationships
- Node.js >= 16.0.0
- Docker and Docker Compose
- MySQL (or use Docker MySQL service)
-
Clone the repository
git clone https://github.com/maisacmoraes/nodejs-api-blog.git cd nodejs-api-blog -
Using Docker (Recommended)
docker-compose up -d --build docker exec -it blogs_api bash npm install npm run dev -
Local Development Setup
npm install npm run predev # Creates database and runs migrations npm run dev # Starts development server
Create a .env file in the root directory:
NODE_ENV=development
API_PORT=3001
DB_HOST=localhost
DB_PORT=3306
DB_NAME=blogs_api
DB_USER=root
DB_PASSWORD=password
JWT_SECRET=your_jwt_secret_herenpm run dev- Start development server with nodemonnpm test- Run all testsnpm run test-coverage- Run tests with coverage reportnpm run lint- Run ESLintnpm run seed- Seed database with sample data
POST /login- User login{ "email": "user@example.com", "password": "123456" }
GET /user- Get all users (requires authentication)GET /user/:id- Get user by ID (requires authentication)POST /user- Create new user{ "displayName": "John Doe", "email": "john@example.com", "password": "123456", "image": "http://example.com/image.jpg" }DELETE /user/me- Delete current user (requires authentication)
GET /categories- Get all categories (requires authentication)POST /categories- Create new category (requires authentication){ "name": "Technology" }
GET /post- Get all posts (requires authentication)GET /post/:id- Get post by ID (requires authentication)GET /post/search?q=searchTerm- Search posts (requires authentication)POST /post- Create new post (requires authentication){ "title": "My Post Title", "content": "Post content here...", "categoryIds": [1, 2] }PUT /post/:id- Update post (requires authentication and ownership)DELETE /post/:id- Delete post (requires authentication and ownership)
All responses follow a consistent JSON format:
{
"id": 1,
"title": "Post Title",
"content": "Post content",
"userId": 1,
"published": "2023-01-01T00:00:00.000Z",
"updated": "2023-01-01T00:00:00.000Z",
"user": {
"id": 1,
"displayName": "John Doe",
"email": "john@example.com",
"image": "http://example.com/image.jpg"
},
"categories": [
{
"id": 1,
"name": "Technology"
}
]
}Database Connection Error
- Ensure MySQL is running (if using local setup)
- Check database credentials in environment variables
- Run
npm run predevto create database and run migrations
Port Already in Use
- Change the
API_PORTin your environment variables - Kill existing processes:
npm run kill:test
JWT Token Issues
- Ensure
JWT_SECRETis set in environment variables - Check token format in Authorization header:
Bearer <token>
Docker Issues
- Ensure Docker and Docker Compose are installed
- Run
docker-compose downthendocker-compose up -d --build
- Use
npm run devfor hot reloading during development - Run
npm run lintbefore committing changes - Use
npm run test-coverageto check test coverage - Check logs in Docker:
docker logs blogs_api
This project is licensed under the MIT License.
Feel free to use, modify, and distribute this project with proper attribution