Skip to content

Simple JSON server using Cloudflare Pages functions. combine-json and json-server combined!

License

Notifications You must be signed in to change notification settings

ynshung/cloudflare-json-server

Repository files navigation

Cloudflare JSON Server

Simple JSON server using Cloudflare Pages functions. Similiar to combine-json and json-server combined!

Note: REST functionality is not fully implemented and may be buggy. Intended for read-only server.

Usage

Clone or fork this repository, install dependencies by running npm i and run npm run dev to host a local API server at http://localhost:8788. Change the contents of the JSON file inside the json directory, reload the page (such as /api/cars) to see changes in realtime.

To host the server in Cloudflare Pages, create a new project there and choose your repository. Use "Create React App" as the framework preset. (Recommened to set NODE_VERSION to higher version such as 16.13.2)

npm run build will create the JSON file at build/db.json. You can also combine with your existing frontend project.

Cloudflare Workers

You can host the backend code separately by changing the api code from export async function onRequestGet({ request }) to:

addEventListener("fetch", event => {
    event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {

Example

You may refer to the documentation of combine-json for the implementation of folder-structured JSON.

Known Issues

  • Does not work as well for keyed object (such as city folder in example), querying and hiding may be broken.