fetch('http://localhost:3000/posts', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ title: "My First Post", content: "This is some data saved to database.json!" }), }) .then(response => response.json()) .then(data => console.log('Success:', data)); Use code with caution. Copied to clipboard 2. Using Node.js (Direct File Writing)
In your terminal, run: npx json-server --watch database.json database.json
To write a new entry (a "post") to a database.json file, you can either use a tool like for a quick mock API or write a script in a language like JavaScript (Node.js) or Python to handle the file operations directly. 1. Using json-server (Quickest for Web Devs) Add the new post db
const fs = require('fs'); const newPost = { id: Date.now(), title: "Direct Write", content: "Added via fs module" }; // 1. Read the existing file const data = fs.readFileSync('database.json'); const db = JSON.parse(data); // 2. Add the new post db.posts.push(newPost); // 3. Write it back fs.writeFileSync('database.json', JSON.stringify(db, null, 2)); Use code with caution. Copied to clipboard 3. Using Python Python is often used for simple JSON "databases": Add the new post db.posts.push(newPost)