CORS errors with json-server usually come down to the version you run. This post targets json-server 0.17.4, the last stable release. v1 is still in beta and works differently.
Why CORS happens with json-server
Browsers enforce the same-origin policy. When your frontend runs on http://localhost:3000 and json-server runs on http://localhost:3001, the browser treats them as different origins because the port is different. Any fetch or XHR request from your frontend to json-server gets blocked unless the server responds with the right CORS headers.
The error looks something like this:
Access to fetch at 'http://localhost:3001/users' from origin 'http://localhost:3000'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present.
What json-server 0.17 already does
The 0.17.4 CLI and jsonServer.defaults() enable CORS out of the box. Send a request with an Origin header to see it:
curl -i -H 'Origin: http://localhost:3000' http://localhost:3001/users
The response echoes the origin back in Access-Control-Allow-Origin and adds Access-Control-Allow-Credentials: true. A preflight OPTIONS request gets a 204 with the methods and the headers the browser asked for. So with 0.17 and no custom setup, CORS works.
If you still see the error, check these first:
- You started the server with
--no-cors(ornoCors: trueindefaults()). - The server isn't running, or the port is wrong. The browser reports that as a CORS error too.
- You're on json-server v1 beta, which sends
Access-Control-Allow-Origin: *and has no scripting API. More on that below.
When you need a custom server.js
Write your own server when you want to control the headers, for example to allow one origin only. Create server.js at your project root:
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
server.use(jsonServer.defaults({ noCors: true }));
server.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
return res.sendStatus(204);
}
next();
});
server.use(router);
server.listen(3001, () => {
console.log('JSON Server is running on port 3001');
});
Run it with:
node server.js
noCors: true turns off the built-in CORS handling so it doesn't fight with yours. The middleware runs before the router, so every response gets those headers.
Why the preflight handler matters
For non-simple requests - anything with a JSON body, a custom header like Authorization, or methods like PUT, PATCH, DELETE - the browser sends a preflight OPTIONS request first to check if the server allows it. The OPTIONS check in the middleware answers that request right away, and the actual request then goes through.
Wildcard vs specific origin
Access-Control-Allow-Origin: * is fine for local dev when you don't send credentials. It allows requests from any origin.
If you need cookies or credentials: 'include' in your fetch calls, the browser rejects a wildcard origin. The same goes for Access-Control-Allow-Headers: *, which is treated as a literal header name in credentialed requests. Name the origin and each header, as in the code above.
json-server v1 beta is ESM only and has no require('json-server') or jsonServer.create(). You can only run it as a CLI, and it sends * as the origin. For credentialed requests, stay on 0.17.4 or put a dev proxy such as the Vite server.proxy option in front, so the browser sees one origin.
Conclusion
json-server 0.17 handles CORS by default, so check for --no-cors or a wrong port before writing anything. Write a custom server.js only when you need specific headers, and name the origin and headers explicitly if you send credentials. On v1 beta, use a proxy instead.