@@ -11,6 +11,7 @@ A TypeScript implementation of json-server with additional features and comprehe
1111- Full TypeScript support with type definitions
1212- RESTful API endpoints from a JSON file or JavaScript object
1313- Configurable routes
14+ - API prefix support (` /api/* ` for all routes)
1415- Support for multiple package managers (npm, yarn, pnpm, bun)
1516- CORS support
1617- Delay simulation for network latency testing
@@ -178,6 +179,7 @@ Options:
178179 --read-only, --ro Allow only GET requests [default: false]
179180 --no-cors, --nc Disable CORS [default: false]
180181 --no-gzip, --ng Disable GZIP compression [default: false]
182+ --enable-api-prefix, --api Enable /api/* prefix [default: false]
181183 --delay, -d Add delay to responses (ms) [number]
182184 --id, -i Set database id field [default: "id"]
183185 --foreignKeySuffix Set foreign key suffix [default: "_id"]
@@ -203,6 +205,50 @@ GET /posts?_sort=title&_order=asc
203205GET /posts?_sort=title&_order=desc
204206```
205207
208+ ## API Prefix
209+
210+ The API prefix feature allows you to access all your resources with an ` /api ` prefix. This is useful when:
211+
212+ - You want to make your mock API feel more like a real backend
213+ - You need to differentiate API routes from other routes in your application
214+ - You're working with frontend frameworks that expect API routes to start with ` /api `
215+
216+ ### Using API Prefix with CLI
217+
218+ Enable the API prefix feature using the ` --enable-api-prefix ` (or ` -api ` shorthand) flag:
219+
220+ ``` bash
221+ json-server db.json --enable-api-prefix
222+ ```
223+
224+ This allows you to access resources through both standard and API-prefixed routes:
225+
226+ ```
227+ # Standard routes still work
228+ GET /posts
229+ GET /posts/1
230+
231+ # API-prefixed routes also work
232+ GET /api/posts
233+ GET /api/posts/1
234+ ```
235+
236+ ### Using API Prefix Programmatically
237+
238+ ``` typescript
239+ import { create } from ' @webmasterdevlin/json-server' ;
240+
241+ const server = create ({
242+ port: 3000 ,
243+ enableApiPrefix: true , // Enable the API prefix feature
244+ });
245+
246+ server .loadDatabase (' ./db.json' );
247+ server .start ().then (() => {
248+ console .log (' Server running with API prefix support' );
249+ });
250+ ```
251+
206252## Programmatic Usage
207253
208254``` typescript
@@ -220,6 +266,7 @@ const server = create({
220266 host: ' localhost' ,
221267 readOnly: false , // Allow all HTTP methods
222268 delay: 1000 , // Add 1s delay to responses
269+ enableApiPrefix: true , // Enable /api/* prefix for all routes
223270});
224271
225272// Create a custom route
0 commit comments