Angular 20 json-server Setup
Goal
Provide a repeatable, repo-friendly setup for a local mock REST API using json-server, integrated with Angular 20 development (ng serve, proxy, scripts, and a sane folder layout).
Recommended Layout
Create this structure at repo root:
/
mocks/
db.json
README.md
src/
proxy.conf.json
package.json
angular.json
Inputs
projectRoot (string, default: current working directory)
apiPort (number, default: 3000)
apiPrefix (string, default: /api)
useConcurrently (boolean, default: true)
addAdvancedServerTemplate (boolean, default: false)
packageManager (string, default: auto-detect from lockfile: npm | pnpm | yarn)
Success Criteria
json-server is installed as a dev dependency.
mocks/db.json exists and serves resources.
src/proxy.conf.json exists and rewrites /api/* to the mock API origin.
angular.json serve options include proxyConfig: "src/proxy.conf.json".
npm run start:mock starts Angular and mock API together.
- Angular uses relative
/api/... calls instead of hardcoded localhost URLs.
Workflow
Preflight and workspace validation
- Resolve
projectRoot to an absolute path.
- Detect package manager from lockfile:
pnpm-lock.yaml -> pnpm
yarn.lock -> yarn
- otherwise ->
npm
- Confirm
angular.json and package.json exist in projectRoot.
- Confirm
@angular/core major version is 20.
- Abort with an error when the project is not Angular 20.
- If monorepo/multi-project, apply proxy and serve updates to the actively used Angular app target; do not modify unrelated targets.
Install dependencies
- Install
json-server as a dev dependency using detected package manager:
npm i -D json-server
- If
useConcurrently is true, install concurrently as a dev dependency:
npm i -D concurrently
- Keep installs local; do not require global packages.
- Do not downgrade or remove unrelated dependency entries.
Create mocks/db.json
- Ensure
mocks/ exists.
- Create
mocks/db.json with valid JSON when missing.
- If it already exists, preserve existing data.
- Prefer this minimal seed when no domain data is provided:
{}
Add npm scripts in package.json
- Ensure these scripts exist (merge safely with existing scripts):
{
"scripts": {
"mock:api": "json-server ./mocks/db.json --port 3000",
"start": "ng serve",
"start:mock": "concurrently -n API,NG \"npm:mock:api\" \"npm:start\""
}
}
- If
useConcurrently is false, set start:mock to only run the API script:
{
"scripts": {
"start:mock": "npm run mock:api"
}
}
- Preserve script names unless the user asks otherwise.
- Update scripts idempotently:
- create missing keys
- replace only targeted script values
- never remove unrelated scripts
Configure Angular proxy
- Create or update
src/proxy.conf.json:
{
"/api": {
"target": "http://localhost:3000",
"secure": false,
"changeOrigin": true,
"logLevel": "debug",
"pathRewrite": {
"^/api": ""
}
}
}
- If
apiPrefix differs from /api, update both the top-level key and pathRewrite consistently.
- In
angular.json, set proxyConfig under the active serve target:
- preferred path:
projects.<name>.architect.serve.options.proxyConfig
- alternate path in newer builders:
projects.<name>.targets.serve.options.proxyConfig
- Do not delete existing serve options when inserting
proxyConfig.
{
"proxyConfig": "src/proxy.conf.json"
}
Apply Angular usage pattern
- Prefer environment-based API base URL in development.
- Set
apiBaseUrl: '/api' in environment.development.ts.
- Build service URLs as
${environment.apiBaseUrl}/resource.
- Do not hardcode
http://localhost:3000 in app code.
Add mock API docs
- Create
mocks/README.md with:
- endpoint list
- run commands (
npm run mock:api, npm run start:mock)
- sample curl commands
- reset instructions (edit or replace
mocks/db.json)
- Include one troubleshooting section for common errors:
Unknown option from json-server
- proxy not applied due to wrong serve target
- hardcoded backend URLs bypassing
/api
Verify setup
npm run start:mock
- Verify process behavior:
- API listens on
apiPort
- Angular serve starts successfully
- Verify integration behavior:
- Angular app can call
${apiPrefix}/... without CORS errors
- resources in
mocks/db.json map to endpoints
- proxy rewrite removes
apiPrefix before reaching json-server
- Verify code hygiene:
- no hardcoded
http://localhost:<apiPort> in Angular data services
- if found, convert calls to relative
${apiPrefix} base URLs
Advanced Rules (Version-Sensitive)
Handle v1 differences explicitly
- Recent
json-server versions in the v1 beta line differ from v0.17.
- Expect changes including:
- ID handling differences (string-like IDs in many scenarios)
- pagination via
_page + _per_page
- removed delay flag behavior
Avoid relying on removed/changed CLI flags
- Do not assume
--delay works in newer versions.
- Do not assume
--routes and --middlewares flags are available across versions.
- If users report
Unknown option, treat it as version/line mismatch first.
Prefer module-based wrapper for advanced behavior
- For auth simulation, custom routes, custom middleware, artificial latency, or reset endpoints:
- create a small Node wrapper (
server.js or mocks/server.js) using json-server as a module
- attach custom middleware and route handlers before delegating to the router
- keep this optional and enabled only when requested
Guardrails
- Merge JSON changes safely; do not overwrite unrelated config.
- Keep edits idempotent; running this skill multiple times should converge to the same working state.
- Keep API URLs in Angular app relative (
/api/...).
- Keep
mocks/db.json committed for reproducibility.
- Prefer small, explicit defaults over broad scaffolding.
- If conflicting existing conventions are present, preserve them unless they break success criteria.
Assistant Portability Rules
- Drive edits from on-disk config and lockfiles; do not assume a package manager or Angular target without checking.
- Keep script/proxy mutations idempotent and narrowly scoped to required keys.
- If active serve target resolution is ambiguous, stop with one explicit blocker and required target path.
Definition of Done
npm run start:mock launches Angular + mock API.
- Angular calls to
/api/... succeed through proxy without CORS issues.
- Endpoints are generated from
mocks/db.json.
- Mock API run/reset/extension steps are documented in
mocks/README.md.
- Re-running setup does not corrupt scripts or Angular serve configuration.
References
1---2name: angular-json-server-setup3description: Add, repair, or verify a local json-server mock REST API for Angular 20 workspaces, including deterministic dependency setup, `mocks/db.json`, proxy wiring (`src/proxy.conf.json` + `angular.json`), npm scripts, and mock API docs. Use when users ask to create/fix `/api` proxy flows, run Angular+API together, troubleshoot json-server option/version mismatches, or add advanced middleware/routes/auth simulation.4---5
6# Angular 20 json-server Setup
7
8## Goal
9
10Provide a repeatable, repo-friendly setup for a local mock REST API using `json-server`, integrated with Angular 20 development (`ng serve`, proxy, scripts, and a sane folder layout).
11
12## Recommended Layout
13
14Create this structure at repo root:
15
16```text
17/
18 mocks/
19 db.json
20 README.md
21 src/
22 proxy.conf.json
23 package.json
24 angular.json
25```
26
27## Inputs
28
29- `projectRoot` (string, default: current working directory)
30- `apiPort` (number, default: `3000`)
31- `apiPrefix` (string, default: `/api`)
32- `useConcurrently` (boolean, default: `true`)
33- `addAdvancedServerTemplate` (boolean, default: `false`)
34- `packageManager` (string, default: auto-detect from lockfile: `npm` | `pnpm` | `yarn`)
35
36## Success Criteria
37
38- `json-server` is installed as a dev dependency.
39- `mocks/db.json` exists and serves resources.
40- `src/proxy.conf.json` exists and rewrites `/api/*` to the mock API origin.
41- `angular.json` serve options include `proxyConfig: "src/proxy.conf.json"`.
42- `npm run start:mock` starts Angular and mock API together.
43- Angular uses relative `/api/...` calls instead of hardcoded `localhost` URLs.
44
45## Workflow
46
471. Preflight and workspace validation
48 - Resolve `projectRoot` to an absolute path.
49 - Detect package manager from lockfile:
50 - `pnpm-lock.yaml` -> `pnpm`
51 - `yarn.lock` -> `yarn`
52 - otherwise -> `npm`
53 - Confirm `angular.json` and `package.json` exist in `projectRoot`.
54 - Confirm `@angular/core` major version is `20`.
55 - Abort with an error when the project is not Angular 20.
56 - If monorepo/multi-project, apply proxy and serve updates to the actively used Angular app target; do not modify unrelated targets.
57
582. Install dependencies
59 - Install `json-server` as a dev dependency using detected package manager:
60 ```bash
61 npm i -D json-server
62 ```
63 - If `useConcurrently` is true, install `concurrently` as a dev dependency:
64 ```bash
65 npm i -D concurrently
66 ```
67 - Keep installs local; do not require global packages.
68 - Do not downgrade or remove unrelated dependency entries.
69
703. Create `mocks/db.json`
71 - Ensure `mocks/` exists.
72 - Create `mocks/db.json` with valid JSON when missing.
73 - If it already exists, preserve existing data.
74 - Prefer this minimal seed when no domain data is provided:
75 ```json
76 {}
77 ```
78
794. Add npm scripts in `package.json`
80 - Ensure these scripts exist (merge safely with existing scripts):
81 ```json
82 {
83 "scripts": {
84 "mock:api": "json-server ./mocks/db.json --port 3000",
85 "start": "ng serve",
86 "start:mock": "concurrently -n API,NG \"npm:mock:api\" \"npm:start\""
87 }
88 }
89 ```
90 - If `useConcurrently` is false, set `start:mock` to only run the API script:
91 ```json
92 {
93 "scripts": {
94 "start:mock": "npm run mock:api"
95 }
96 }
97 ```
98 - Preserve script names unless the user asks otherwise.
99 - Update scripts idempotently:
100 - create missing keys
101 - replace only targeted script values
102 - never remove unrelated scripts
103
1045. Configure Angular proxy
105 - Create or update `src/proxy.conf.json`:
106 ```json
107 {
108 "/api": {
109 "target": "http://localhost:3000",
110 "secure": false,
111 "changeOrigin": true,
112 "logLevel": "debug",
113 "pathRewrite": {
114 "^/api": ""
115 }
116 }
117 }
118 ```
119 - If `apiPrefix` differs from `/api`, update both the top-level key and `pathRewrite` consistently.
120 - In `angular.json`, set `proxyConfig` under the active serve target:
121 - preferred path: `projects.<name>.architect.serve.options.proxyConfig`
122 - alternate path in newer builders: `projects.<name>.targets.serve.options.proxyConfig`
123 - Do not delete existing serve options when inserting `proxyConfig`.
124 ```json
125 {
126 "proxyConfig": "src/proxy.conf.json"
127 }
128 ```
129
1306. Apply Angular usage pattern
131 - Prefer environment-based API base URL in development.
132 - Set `apiBaseUrl: '/api'` in `environment.development.ts`.
133 - Build service URLs as `${environment.apiBaseUrl}/resource`.
134 - Do not hardcode `http://localhost:3000` in app code.
135
1367. Add mock API docs
137 - Create `mocks/README.md` with:
138 - endpoint list
139 - run commands (`npm run mock:api`, `npm run start:mock`)
140 - sample curl commands
141 - reset instructions (edit or replace `mocks/db.json`)
142 - Include one troubleshooting section for common errors:
143 - `Unknown option` from `json-server`
144 - proxy not applied due to wrong serve target
145 - hardcoded backend URLs bypassing `/api`
146
1478. Verify setup
148 - Run:
149 ```bash
150 npm run start:mock
151 ```
152 - Verify process behavior:
153 - API listens on `apiPort`
154 - Angular serve starts successfully
155 - Verify integration behavior:
156 - Angular app can call `${apiPrefix}/...` without CORS errors
157 - resources in `mocks/db.json` map to endpoints
158 - proxy rewrite removes `apiPrefix` before reaching json-server
159 - Verify code hygiene:
160 - no hardcoded `http://localhost:<apiPort>` in Angular data services
161 - if found, convert calls to relative `${apiPrefix}` base URLs
162
163## Advanced Rules (Version-Sensitive)
164
1651. Handle v1 differences explicitly
166 - Recent `json-server` versions in the v1 beta line differ from v0.17.
167 - Expect changes including:
168 - ID handling differences (string-like IDs in many scenarios)
169 - pagination via `_page` + `_per_page`
170 - removed delay flag behavior
171
1722. Avoid relying on removed/changed CLI flags
173 - Do not assume `--delay` works in newer versions.
174 - Do not assume `--routes` and `--middlewares` flags are available across versions.
175 - If users report `Unknown option`, treat it as version/line mismatch first.
176
1773. Prefer module-based wrapper for advanced behavior
178 - For auth simulation, custom routes, custom middleware, artificial latency, or reset endpoints:
179 - create a small Node wrapper (`server.js` or `mocks/server.js`) using `json-server` as a module
180 - attach custom middleware and route handlers before delegating to the router
181 - keep this optional and enabled only when requested
182
183## Guardrails
184
185- Merge JSON changes safely; do not overwrite unrelated config.
186- Keep edits idempotent; running this skill multiple times should converge to the same working state.
187- Keep API URLs in Angular app relative (`/api/...`).
188- Keep `mocks/db.json` committed for reproducibility.
189- Prefer small, explicit defaults over broad scaffolding.
190- If conflicting existing conventions are present, preserve them unless they break success criteria.
191
192## Assistant Portability Rules
193
194- Drive edits from on-disk config and lockfiles; do not assume a package manager or Angular target without checking.
195- Keep script/proxy mutations idempotent and narrowly scoped to required keys.
196- If active serve target resolution is ambiguous, stop with one explicit blocker and required target path.
197
198## Definition of Done
199
200- `npm run start:mock` launches Angular + mock API.
201- Angular calls to `/api/...` succeed through proxy without CORS issues.
202- Endpoints are generated from `mocks/db.json`.
203- Mock API run/reset/extension steps are documented in `mocks/README.md`.
204- Re-running setup does not corrupt scripts or Angular serve configuration.
205
206## References
207
208[1]: https://github.com/typicode/json-server#readme
209[2]: https://angular.dev/tools/cli/serve
210[3]: https://angular.dev/tools/cli/environments
211[4]: https://docs.npmjs.com/cli/v10/commands/npm-install