A simple backend API for managing articles.
This repository contains a Node.js backend API for articles. It allows users to perform CRUD operations on articles, including creating, reading, updating, and deleting articles.
- Create a new article with a title, description, and cover image.
- Retrieve a list of all articles.
- Retrieve a single article by its ID.
- Update an existing article's title, description, and cover image.
- Delete an article by its ID.
- Validate object exist or not when create or update.
- Node.js
- Express.js
- Sequelize (for database ORM)
- Multer (for file uploads)
- Mysql (you can mention your preferred database here)
- Other dependencies (list any other major dependencies)
To run this project locally, follow these steps:
- Clone the repository:
git clone https://github.com/apisitas/API-Article.git
- Install dependencies:
npm install
- Configure the database connection in
.env
file (if required) you can see env example file(.env.example). - Run the migrations and seed the database (if applicable):
npx sequelize-cli db:migrate
thennpx sequelize-cli db:seed:all
- Start the server:
npm run server
- The API should now be accessible at
http://localhost:5000
. if want see image you have to combine url and path togeter for example 'http://localhost:5000/uploads/1691079127824-churros-g89bd8c720_1280.jpg'
- Endpoint:
GET /api/articles?page=1&size=100
- Description: Get a list of all articles (if not set size the default will be 2 for pagination and if not set page the default will be 1).
- Noted: The publisher object will display in json and if cover is null backend has been set getter in model Article so the cover will be 'assets/no-image-icon-23494.png'
- Response: JSON array containing all articles.
- Endpoint:
GET /api/articles/:id
- Description: Get a single article by its ID totalView will update everytime when access to this endpoint.
- Noted: The publisher object will display in json
- Response: JSON object representing the article.
- Endpoint:
POST /api/articles
- Description: Create a new article. (Only png, jpeg, jpg, and gif images are supported)
- Request Body: JSON object containing
title
,description
,cover
andpublisherId
fields. - Response: JSON object and text 'Article created was a success'.
- Endpoint:
PUT /api/articles/:id
- Description: Update an existing article by its ID (If add new cover then backend will remove old cover and Only png, jpeg, jpg, and gif images are supported).
- Request Body: JSON object containing
title
,description
,cover
andpublisherId
fields. - Response: JSON object and text 'Article updated was a success'.
- Endpoint:
DELETE /api/articles/:id
- Description: Delete an article by its ID (Backend will also remove that cover in server).
- Response: JSON object and text 'Remove Article was a success'