Welcome to the documentation for the Serverless Blog API. This API allows you to create, read, update, and delete blog posts through various endpoints, showcasing the functionality of backend development using Google Cloud Functions, MongoDB, and Node.js.
The Serverless Blog API is a demonstration project that showcases how to build a simple backend using Google Cloud Functions, MongoDB, and Node.js. The API provides endpoints for performing CRUD (Create, Read, Update, Delete) operations on blog posts. It aims to demonstrate the principles of RESTful API design while leveraging serverless architecture.
To use the Serverless Blog API, you'll need the following:
- A Google Cloud Platform account
- A MongoDB instance or cluster
- Node.js and npm installed on your local machine
Follow these steps to set up and run the API:
- Clone the project repository:
git clone https://github.com/jay-agrawat/serverless-blog-api
cd serverless-blog-api
Note : Currently Above Repository is Private
- Install the project dependencies:
npm install
- Configure environment variables:
Create a
.env
file in the root directory and define the following variables:
MONGODB_URI=your-mongodb-uri
- Deploy the Cloud Functions: Deploy the Cloud Functions using the Google Cloud SDK or other deployment methods.
gcloud functions deploy blog_api_v1 --gen2 --runtime=nodejs20 --region=us-central1 --source=. --entry-point=app --trigger-http --allow-unauthenticated --memory 512Mi --max-instances=1
Base URL: https://us-central1-abiding-sunset-390217.cloudfunctions.net/blog_api_v1
-
Method: POST
-
Endpoint:
/posts
-
Request Body:
{
"title": "Sample Title",
"content": "Sample Content",
"author": "John Doe"
}
- Response:
201 Created
with the newly created post ID.
- Method: GET
- Endpoint:
/posts
- Response:
200 OK
with an array of blog post objects.
- Method: GET
- Endpoint:
/posts/{postId}
- Response:
200 OK
with the blog post object or404 Not Found
if the post doesn't exist.
-
Method: PUT
-
Endpoint:
/posts/{postId}
-
Request Body:
{
"title": "Updated Title",
"content": "Updated Content"
}
- Response:
200 OK
with a success message.
- Method: DELETE
- Endpoint:
/posts/{postId}
- Response:
200 OK
with a success message.
The API provides meaningful error responses for various scenarios, including missing or invalid input data, resource not found, and internal server errors. The appropriate HTTP status codes are used to indicate the outcome of each request.
Deploying the Cloud Functions can be done using the Google Cloud SDK or other deployment methods. Make sure to configure the necessary environment variables for connecting to your MongoDB instance.
gcloud functions deploy hello-node-function \
--gen2 \
--runtime=nodejs20 \
--region=us-central1 \
--source=. \
--entry-point=helloGET \
--trigger-http \
y--memory 512Mi --max-instances=1
The Serverless Blog API is a practical example of building a backend using Google Cloud Functions, MongoDB, and Node.js. It demonstrates how to implement CRUD operations and follows RESTful API design principles. Feel free to use this project as a foundation for learning and building more advanced applications.