A simple serverless blog API using Google Cloud Functions and Nodejs. This project will allow us to create, read, update, and delete blog posts through API endpoints. This project illustrates the use of Google cloud functions.
- Tech stack used: Nodejs, Expressjs, MongoDb, Google Cloud Functions and Javascript
$ MongoDb
$ Nodejs
$ gcould-sdk
# Method to run serverless api on local
- install node version 18
- run npm i in root directory
- setup your mongo atlas credentials in .env file
- npm run start
# The function will be accessable on localhost:8080 endpoint
- Google cloud function has been implemented to serve as api endpoint.
- Deployment done on google cloud platform by using
gcloud-sdk
. - Below is the Api endpoint for the hosted functions.
# url
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts
- Api Details Can be found below
-
Get all blog posts (method GET)
- endpoint ->
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts
curl --location 'https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts'
# Success [ { "_id": "64e6723ec230e37f155bdc11", "title": "Basketball", "content": "ibasketball is a highly intensive game", "author": "James", "publicationDate": "2023-08-23T18:37:57.962Z", "isArchived": false, "createdAt": "2023-08-23T20:55:26.485Z", "updatedAt": "2023-08-23T20:55:26.485Z", "__v": 0 }, { "_id": "64e67356c230e37f155bdc16", "title": "basketball", "content": "It is a very intensive game", "author": "James", "publicationDate": "2023-08-23T18:37:57.962Z", "isArchived": false, "createdAt": "2023-08-23T21:00:06.397Z", "updatedAt": "2023-08-23T21:00:06.397Z", "__v": 0 } ]
- endpoint ->
-
Get blog post by id (method GET)
- endpoint ->
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/:id
curl --location 'https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/64e66c065e2a21f9211da3f9'
# Success { "_id": "64e67eec19905f881a724087", "title": "Cricket", "content": "It is a very intensive game", "author": "Kohli", "publicationDate": "2023-08-23T18:37:57.962Z", "isArchived": false, "createdAt": "2023-08-23T21:49:32.632Z", "updatedAt": "2023-08-23T21:49:32.632Z", "__v": 0 } # Failures { "error": "Invalid post ID" } { "error": "Post not found" }
- endpoint ->
-
Create new blog post (method POST)
- endpoint ->
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/
curl --location 'https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/' \--header 'Content-Type: application/json' \--data '{ "title": "basketball", "content": "It is a very intensive game", "author": "James", "publicationDate": "2023-08-23T18:37:57.962Z"}'
{ "title": "basketball", "content": "It is a very intensive game", "author": "James", "publicationDate": "2023-08-23T18:37:57.962Z" }
# Success { "title": "basketball", "content": "It is a very intensive game", "author": "James", "publicationDate": "2023-08-23T18:37:57.962Z", "isArchived": false, "_id": "64e67356c230e37f155bdc16", "createdAt": "2023-08-23T21:00:06.397Z", "updatedAt": "2023-08-23T21:00:06.397Z", "__v": 0 } # Failure (validations failures) { "error": "BlogPost validation failed: publicationDate: Path `publicationDate` is required., title: Path `title` is required." }
- endpoint ->
-
Update new blog post by Id (method PUT)
- endpoint ->
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/:id
curl --location --request PUT 'https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/64e67356c230e37f155bdc16' \--header 'Content-Type: application/json' \--data '{ "title": "tennis", "author": "Federer"}'
{ "title": "tennis", "author": "Federer" }
# Success { "title": "tennis", "content": "It is a very intensive game", "author": "Federer", "publicationDate": "2023-08-23T18:37:57.962Z", "isArchived": false, "_id": "64e67356c230e37f155bdc16", "createdAt": "2023-08-23T21:00:06.397Z", "updatedAt": "2023-08-23T21:01:58.403Z", "__v": 0 } # Failures { "error": "Invalid post ID" } { "error": "Post not found" }
- endpoint ->
-
Delete a blog post by Id (method DELETE)
- endpoint ->
https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/:id
curl --location --request DELETE 'https://us-central1-fluted-magpie-396811.cloudfunctions.net/gcp-blogpost-func/posts/64e67eec19905f881a724087'
# Success { "message": "Successfully deleted!" } # Failures { "error": "Invalid post ID" } { "error": "Post not found" }
- endpoint ->
{
# all fields are kept mandatory
id: ObjectId; (unique)
title: string;
content: string;
author: string;
isArchived: boolean;
publicationDate: Date;
createdAt: Date,
updatedAt: Date
}