-Tech Stack: Node.js, Express, PostgresSQL, pgAdmin4 -This backend application should enable blog management for posts and comments, including creating, retrieving, updating, and deleting. Creating, retrieving, updating, and deleting is supported for blog posts. As for comments, retrieving and creating is supported.
GET /posts: Retrieves all posts.GET /posts/{id}: Retrieves a specific post by ID.POST /posts: Makes new post. Requires several parameters includingtitle,content, andauthorin the request body.PUT /posts/{id}: Updates the post with the ID. Requirestitle,content, andauthorin the request bodyDELETE /posts/{id}: Deletes a post with the ID in the parameter
GET /posts/{postId}/comments: Retrieves all comments for the specified post.POST /posts/{postId}/comments: Adds a new comment to the specified post. Requirescontentandauthorin the request body.
-
Download and start PostgreSQL and ensure it's running
-
Download and start pgAdmin4. Connect to PostgreSQL server and create new database named "blog"
-
Create tables below:
CREATE TABLE posts ( id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, author VARCHAR(255) NOT NULL ); CREATE TABLE comments ( id SERIAL PRIMARY KEY, postId INTEGER NOT NULL, content TEXT NOT NULL, author VARCHAR(255) NOT NULL, FOREIGN KEY (postId) REFERENCES posts(id) ON DELETE CASCADE );
-Insert sample data:
INSERT INTO posts (title, content, author) VALUES ('My first post by Alice!', 'this is my first post! ', 'Alice'),
('I have the second post !', 'John is the second post!', 'John'),
('Last Post', 'My name is jeffrey!', 'Jeffrey');
INSERT INTO comments (postId, content, author) VALUES
(1, 'Great post Alice!', 'John'),
(1, 'Very Nice Alice!', 'Jeffrey');
INSERT INTO comments (postId, content, author) VALUES
(2, 'Keep it up!', 'Alice'),
(2, 'Good job John!', 'Jeffrey');
INSERT INTO comments (postId, content, author) VALUES
(3, 'Good one Jeffrey!', 'Alice'),
(3, 'Thanks for the post!', 'John')
-
Clone this repository
-
install dependencies in terminal 'npm install'
-
set up db.js ( sample code is provided in the repository and may need to be modified). Replace user, host, database, password, and port as needed
const pool = new Pool({ user: 'test_user', host: 'localhost', database: 'blog', password: 'test_password', port: 5432 })
- Run application by typing in terminal "node app.js"
- Copy-paste 'http://localhost:3000' to web browser of your choice
- Download and open Postman to configure and test request to API endpoints
- Verify responses in Postman to ensure that each individual API endpoints works as intended
- To ensure project works as intended, ensure to have all parts done