This is a RESTful API for a simple blogging platform, built using Node.js, Express, MySQL, and JWT for authentication. The API allows users to perform CRUD operations (Create, Read, Update, Delete) on blog posts and includes token-based authentication for secure access.
- User Registration and Login
- Create, Read, Update, and Delete Blog Posts
- Token-Based Authentication
- Node.js
- Express.js
- MySQL
- JWT (JSON Web Token)
- bcryptjs
- Node.js installed on your machine
- MySQL server running
- Clone the repository:
git clone https://github.com/your-username/blog-api.git cd blog-api
- Install dependencies:
npm install
- Set up the database:
CREATE DATABASE blog; USE blog; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL ); CREATE TABLE posts ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) );
- Run the server:
node app.js
- Register a new user
- URL: /api/register
- Method: POST
- Body:
{ "username": "testuser", "password": "testpassword" }
- Login and get a token
- URL: /api/login
- Method: POST
- Body:
{ "username": "testuser", "password": "testpassword" }
- Create a new blog post
- URL: /api/posts
- Method: POST
- Headers:
- x-access-token: your_jwt_token
- Body:
{ "title": "My First Post", "content": "This is the content of my first post." }
- Retrieve a list of all blog posts
- URL: /api/posts
- Method: GET
- Retrieve a single blog post by its ID
- URL: /api/posts/:id
- Method: GET
- Update an existing blog post
- URL: /api/posts/:id
- Method: PUT
- Headers:
- x-access-token: your_jwt_token
- Body:
{ "title": "Updated Post Title", "content": "This is the updated content." }
After update
- Delete a blog post
- URL: /api/posts/:id
- Method: DELETE
- Headers:
- x-access-token: your_jwt_token
After Delete