This project is a Node.js Express API server designed for managing book posts.
are maintainers for this repository.
Before running this server, ensure you have the following installed:
-
Clone the repository:
git@github.com:MaksimShupta/FullstackBlog_backend.git cd FullstackBlog_backend -
Install dependencies:
npm install
To start the server, run the following command:
npm run devThe server will start running at http://localhost:5001
The API endpoints are documented.Here’s how to access it:
The following endpoints are available:
- POST /api/posts Create a new post.
- GET /api/posts Get all posts.
- GET /api/posts/:id Get a single post by ID.
- PUT /api/posts/:id Update an existing post.
- DELETE /api/posts/:id Delete an post by ID.
Environment-specific configurations are set in .env file. This file is not available.
Create a new .env file, you may add the following variable the DATABASE_URL and PORT values.
-
Set up a Node.js server using the built-in http module in package.json.
"type": "module", -
The pg package connect your PostgreSQL database. Variable db , create new instance Pool. Poll coming from pg.
- db test database connection on startup. Print
"connected db"if client is connected or"error db", err.messageif connection do not work.
- db test database connection on startup. Print
We use Neon with postgresql.
Set the following queries in Neon.
-
CREATE TABLE IF NOT EXISTS posts ( id SERIAL PRIMARY KEY, author VARCHAR(255) NOT NULL, title VARCHAR(510) NOT NULL, content VARCHAR(510) NOT NULL, cover VARCHAR(510) NOT NULL, category VARCHAR(255) NOT NULL, date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, createAT TIMESTAMP WITH TIME ZONE, updatedAT TIMESTAMP WITH TIME ZONE )
-
DROP TABLE IF EXISTS posts;
-
SELECT * FROM posts;
-
INSERT INTO posts (author,title, content, cover, category) VALUES ('author','title', 'content', 'cover' , ' category')
-
Description: Retrieve all posts.
-
Response: A JSON array of all posts
-
Description: Retrieve a single post by ID.
-
Response: A JSON object of the post.
-
Description: Create a new post.
-
Request Body: JSON object with post fields.
-
Response: The newly created post as a JSON object.
-
Description: Update an existing post by ID.
-
Request Body: JSON object with post fields.
-
Response: The updated post as a JSON object.
-
Description: Delete a post by ID.
-
Response: A success message or the deleted post as a JSON object.
- Express.js is a fast, minimalist web framework for Node.js that simplifies building APIs and web applications.
- Dotenv is a Node.js package that loads environment variables from a .env file into process.env for secure configuration management.
- pg is a PostgreSQL client for Node.js that enables seamless interaction with PostgreSQL databases using JavaScript.
- cors (Cross-Origin Resource Sharing) is a middleware for Node.js that enables secure cross-origin requests in web applications.