This project is the backend part of a modern news portal application. It is developed using Express.js, TypeScript, PostgreSQL, and Sequelize ORM.
- 🔐 JWT-based authentication and authorization
- 📰 News CRUD operations
- 🗂️ Category management
- 💬 Comment system
- 👍 Like and reaction system
- 📊 API documentation (Swagger UI)
- 🔄 External news API integration
- Express.js: Web framework
- TypeScript: For type safety
- PostgreSQL: Database
- Sequelize ORM: For database operations
- Sequelize-TypeScript: For TypeScript support
- JWT: For authentication
- bcrypt: For password hashing
- Swagger UI: For API documentation
- dotenv: For environment variables
- cors: For CORS operations
- helmet: For security
- Node.js (v14 or higher)
- npm or yarn
- PostgreSQL (v12 or higher)
- Clone the repository:
git clone https://github.com/yourusername/news-backend.git
cd news-backend- Install dependencies:
npm install
# or
yarn install- Create PostgreSQL database:
CREATE DATABASE news_db;- Set environment variables. Create a
.envfile in the root directory:
# Application
PORT=3000
NODE_ENV=development
# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=news_db
DB_USER=postgres
DB_PASSWORD=yourpassword
# JWT
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRES_IN=24h
# External News API
NEWS_API_KEY=your_news_api_key- Compile TypeScript code:
npm run build
# or
yarn build- Create database tables:
npm run db:sync
# or
yarn db:sync- Start the application:
npm run start
# or
yarn start- Run in development mode:
npm run dev
# or
yarn devYou can access the API documentation via browser:
http://localhost:3000/api-docs
POST /api/auth/register- Register new userPOST /api/auth/login- User loginGET /api/auth/profile- View user profilePUT /api/auth/profile- Update user profilePUT /api/auth/change-password- Change user password
GET /api/news- Get all newsGET /api/news/:id- Get specific newsPOST /api/news- Create new news (admin)PUT /api/news/:id- Update news (admin)DELETE /api/news/:id- Delete news (admin)GET /api/news/fetch/external- Fetch news from external API (admin)
GET /api/categories- Get all categoriesGET /api/categories/:id- Get specific categoryPOST /api/categories- Create new category (admin)PUT /api/categories/:id- Update category (admin)DELETE /api/categories/:id- Delete category (admin)
GET /api/comments/news/:newsId- Get comments for a news articlePOST /api/comments- Create new commentPUT /api/comments/:id- Update commentDELETE /api/comments/:id- Delete comment
GET /api/reactions/news/:newsId- Get reactions for a news articleGET /api/reactions/user/news/:newsId- Get user's reaction to a news articlePOST /api/reactions- Add/update reactionDELETE /api/reactions/news/:newsId- Remove reaction
/src
/config # Configuration files
/interfaces # TypeScript interfaces
/middlewares # Express middlewares
/services # Services
/auth # Authentication service
/controllers # Controllers
/models # Sequelize models
/routes # Express routes
/news # News service
/comment # Comment service
/utils # Helper functions
index.ts # Main entry file
┌─────────────┐ ┌─────────────┐
│ Users │ │ News │
├─────────────┤ ├─────────────┤
│ id │ │ id │
│ username │ │ title │
│ email │ │ content │
│ password │ │ imageUrl │
│ role │ │ author │
│ createdAt │ │ source │
│ updatedAt │ │ publishedAt │
└─────────────┘ │ createdAt │
│ │ updatedAt │
│ └─────────────┘
│ │
│ ┌─────────────┐
│ │NewsCategories│
│ ├─────────────┤
│ │ newsId │
│ │ categoryId │
│ └─────────────┘
│ │
┌─────────────┐ ┌─────────────┐
│ Comments │ │ Categories │
├─────────────┤ ├─────────────┤
│ id │ │ id │
│ content │ │ name │
│ userId │ │ slug │
│ newsId │ │ description │
│ createdAt │ │ createdAt │
│ updatedAt │ │ updatedAt │
└─────────────┘ └─────────────┘
│
┌─────────────┐
│ Reactions │
├─────────────┤
│ id │
│ userId │
│ newsId │
│ type │
│ createdAt │
│ updatedAt │
└─────────────┘
Model not initialized error
If you get a "Model not initialized: Member cannot be called" error, make sure that models are properly added to the Sequelize instance in your database.ts file:
// config/database.ts
import { Sequelize } from "sequelize-typescript";
import User from "../services/auth/models/user.model";
import News from "../services/news/models/news.model";
// Import other models
const sequelize = new Sequelize({
// database configuration...
models: [User, News /* other models... */],
});This project is licensed under the MIT License.