/news-api

Primary LanguageJavaScript

News Portal API

This repository contains the News Portal API, a backend application built with Node.js, Express, and Sequelize for managing news articles and categories, complete with JWT-based authentication.

Table of Contents

Project Overview

This API provides endpoints for managing news articles, categories, and user authentication. The application uses Sequelize as the ORM to manage MySQL databases.

Solutions Implemented

  • Table Dependencies in Tests: Resolved dependency issues between Category and News tables by adding cascading deletion (onDelete: 'CASCADE').
  • Test Environment Configuration: Added a config.js to manage separate configurations for development and test environments.
  • Automated Database Reset: The test suite begins by dropping all tables using sequelize.drop().
  • Error Handling for Authentication: Updated the authentication controller to return specific error messages for invalid credentials.

Getting Started

Prerequisites

Make sure you have the following installed:

  • Node.js v18.8.0
  • MySQL
  • Sequelize CLI

Installation

Clone the repository and install dependencies:

git clone <repository-url>
cd <repository-folder>
npm install

Database Setup

Create the MySQL databases for development and testing as specified in config/config.js. Set up your environment variables accordingly.

Migrations and Seeding

To run migrations and seed data:

npx sequelize-cli db:migrate
npx sequelize-cli db:seed:all

Testing

The test suite is built with Jest and Supertest.

Running Tests

To run the tests, ensure the test database is set up in config/config.js, then run:

npm test

Deployment

Environment Variables

Create a .env file with the following variables:

NODE_ENV=production
DB_HOST=<your-database-host>
DB_NAME=<your-database-name>
DB_USER=<your-database-username>
DB_PASS=<your-database-password>
JWT_SECRET=<your-jwt-secret>
PORT=3000

Starting the Server

To deploy locally or on a server:

NODE_ENV=production node app.js

License

This project is open-source and available under the MIT License.