This API is used by the Social Media Application to add/list posts, vote and comment posts. The router used is itty-router
. Data is stored in KV
, a key-value database provided by Cloudflare
. Refer Workers documentation
to get an idea of how workers work.
The APIs accepts data in JSON.
You have to set up a file named wrangler.toml
in the root of your file to start the API. It should contain keys as listed below.
name = "social-media-api"
type = "webpack"
account_id = "***YOUR_ACCOUNT_ID"
workers_dev = true
route = ""
zone_id = ""
compatibility_date = "**compatibility_date**"
kv_namespaces = [
{ binding = "POSTS_KV", preview_id = "**kv_preview_id**", id = "**kv_id**" }
]
[env.production]
kv_namespaces = [
{ binding = "POSTS_KV", id = "**kv_id_for_production**"}
]
{
"status": 404,
"error": "Post not found"
}
[
{
"id": "Slash-1637743242549",
"title": "Post Title",
"username": "Slash",
"content": "Post description",
"comments": [
{
"id": "Slash-1637743258827",
"text": "Inspirational post!",
"username": "Slash"
}
],
"votes": {
"count": 1,
"map": {
"Slash": 1
}
},
"media": {}
}
]
{
"title": "My first post",
"content": "Post content",
"username": "Slash"
}
{
"status": 200,
"message": "Success"
}
{
"username": "Slash",
"comment": "Great post!!"
}
{
"status": 200,
"message": "Success"
}
{
"username": "Slash",
"vote": 1
}
{
"status": 200,
"message": "Success"
}
Run the below command to start your development server.
wrangler dev
The API will be server in http://127.0.0.1:8787
Run the below command to start your development server.
wrangler publish --env production