send data to json file with javascript fetch api

i have one empty json file. I want to add json data to my json file when button is clicked. but it gives error 405. how can I do?

error:

POST net::ERR_ABORTED 405 (Method Not Allowed)

code:

var data={
                        method:"POST",
                        body:{
                            userId:1,
                            title:"sample title",
                            body:"sample body"
                        },
                        headers:new Headers({
                            'content-type':'application/json',
                            'dataType': 'json'
                        })
                    }

           

                    fetch("anaveri.json",data).
                    then(res=>{
                        console.log(res);
                    }).
                    catch(error=>{
                        console.log(error);
                    });

The browser isn't able to directly write data to the server's file system.

It would be a horrible security problem if it could. Google's homepage would get overwritten with some different bit of trolling every few seconds!

The browser can send an HTTP request to a URL. The server then needs to use server side programming to process that request.

You need to pick a programming language that your server supports (or change servers to one that supports your server-side language of choice) and write a webservice that takes the data from the request and stores it.

You could have it write directly to a JSON file, but that risks "fun" problems with concurrent writes so it is more typical to store the data in a database and have another webservice generate the JSON on demand.

You should consider adding some sort of tests (e.g. password authentication and data validation) to control who can insert new data and what sort of data they can insert to avoid the aforementioned vandalism problem.