Welcome to Blog Posts API! This project is a simple API for managing blog posts, similar to a very basic Medium platform. It’s built using Node.js, Express, and includes tests with Jest and Supertest. If you're new to building APIs, this is a great place to start!
This project provides a simple API for creating, reading, updating, and deleting (CRUD) blog posts. By running this code, you’ll set up an API server that lets you:
- View a list of all posts
- Add new posts
- Retrieve a single post by ID
- Update posts by ID
- Delete posts by ID
- Basic CRUD operations for posts
- Stores data temporarily in memory (no database required)
- Uses JSON format for data handling
- Fully tested with Jest and Supertest
- Node.js - JavaScript runtime environment
- Express - Web framework for building APIs
- CORS - Middleware to handle Cross-Origin Resource Sharing
- Body-Parser - Middleware to parse incoming JSON request bodies
- Jest - Testing framework
- Supertest - Tool for HTTP assertions in tests
- Have Node.js installed
- Basic familiarity with JavaScript
- Clone the repository:
git clone https://github.com/MariferVL/blog-post-api.git
- Go to the project directory:
cd blog-post-api - Install all required packages:
npm install
To start the API server, open your terminal in the project directory and run:
node server.jsThe server should start at http://localhost:3000, and you'll see a message confirming that it’s running.
Here’s a quick look at the files included in this project:
├── posts/
│ └── posts.js # Routes for handling blog posts
├── tests/
│ └── server.test.js # Test cases for our API
├── server.js # Main server file
└── package.json # Project configuration and dependencies
- server.js: This file sets up the Express server, connects middleware (CORS, body-parser), and loads our routes for handling blog posts.
- posts/posts.js: This file contains all the routes for our blog posts. It includes routes to get, add, update, and delete posts.
- tests/server.test.js: This file contains tests to check that our API is working correctly. It uses Jest and Supertest to make sure each endpoint does what it’s supposed to.
Each endpoint lets us interact with our blog posts in different ways.
- URL:
/api/posts - Method:
GET - Description: Returns a list of all posts.
- URL:
/api/posts/:id - Method:
GET - Description: Returns a single post by its ID.
- URL:
/api/posts - Method:
POST - Description: Creates a new post.
- Request Body:
{ "title": "Post Title", "content": "Post Content" }
- URL:
/api/posts/:id - Method:
PUT - Description: Updates an existing post.
- Request Body:
{ "title": "Updated Title", "content": "Updated Content" }
- URL:
/api/posts/:id - Method:
DELETE - Description: Deletes a post by its ID.
Testing is an important step to make sure everything in your API works as expected. This project includes tests that cover the functionality of each endpoint.
-
Create the
tests/server.test.jsfile with the following code:const request = require('supertest'); const express = require('express'); const postsRoutes = require('../posts/posts'); const app = express(); app.use(express.json()); app.use('/api/posts', postsRoutes); describe('Posts API', () => { it('should get all posts', async () => { const res = await request(app).get('/api/posts'); expect(res.statusCode).toEqual(200); expect(Array.isArray(res.body)).toBe(true); }); it('should create a new post', async () => { const newPost = { title: 'Test Post', content: 'Test Content' }; const res = await request(app).post('/api/posts').send(newPost); expect(res.statusCode).toEqual(201); expect(res.body.title).toBe('Test Post'); }); it('should get a post by ID', async () => { const res = await request(app).get('/api/posts/1'); expect(res.statusCode).toEqual(200); expect(res.body).toHaveProperty('id', 1); }); it('should update a post by ID', async () => { const updatedPost = { title: 'Updated Title', content: 'Updated Content' }; const res = await request(app).put('/api/posts/1').send(updatedPost); expect(res.statusCode).toEqual(200); expect(res.body.title).toBe('Updated Title'); }); it('should delete a post by ID', async () => { const res = await request(app).delete('/api/posts/1'); expect(res.statusCode).toEqual(204); }); });
-
Run Tests: To execute the tests, open your terminal and run:
npm testThe test results will show whether each endpoint works as expected.
This project is licensed under the ISC License.