%% generate tags start %%
#software-engineering
%% generate tags end %%
#software-engineering/api-development
> [!info] see more
> [json-server - npm (npmjs.com)](https://www.npmjs.com/package/json-server)
```
npm install -g json-server
```
Create a `db.json` file with some data
```json
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
```
Start JSON Server
```shell
json-server --watch db.json
```
Now if you go to [http://localhost:3000/posts/1](http://localhost:3000/posts/1), you'll get
```json
{ "id": 1, "title": "json-server", "author": "typicode" }
```
## Static File Server
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory or use `--static` to set a different static files directory.
```shell
mkdir public
echo 'hello world' > public/index.html
json-server db.json
```
```shell
json-server db.json --static ./some-other-dir
```
## Alternative Port
You can start JSON Server on other ports with the `--port` flag:
```shell
$ json-server --watch db.json --port 3004
```
## Access From Anywhere
You can access your fake API from anywhere using CORS and JSONP.
## Remote Schema
You can load remote schemas.
```shell
$ json-server http://example.com/file.json
$ json-server http://jsonplaceholder.typicode.com/db
```
## Generate Random Data
Using JS instead of a JSON file, you can create data programmatically.
```js
// index.js
module.exports = () => {
const data = { users: [] }
// Create 1000 users
for (let i = 0; i < 1000; i++) {
data.users.push({ id: i, name: `user${i}` })
}
return data
}
```
```shell
$ json-server index.js
```
## How to Deploy?
### On Vercel
✅ this is probably the easiest, see [HananoshikaYomaru/json-server-test (github.com)](https://github.com/HananoshikaYomaru/json-server-test)
### On Railway
> [!info] using docker
> [Dockerfiles | Railway Docs](https://docs.railway.app/deploy/dockerfiles)
### On Fly
### On Render
[Deploy via Dockerfile · Fly Docs](https://fly.io/docs/languages-and-frameworks/dockerfile/)
### Or Do it on Localhost and Expose Your Localhost
see [[How to expose your localhost?]]