A TypeScript implementation of json-server with additional features and comprehensive TypeScript types.
Features
- Full TypeScript support with comprehensive type definitions
- RESTful API endpoints from a JSON file or JavaScript object
- API prefix support - Access all routes with /api/* prefix for production-like experience
- Configurable routes with custom route definitions
- Multiple package managers support (npm, yarn, pnpm, bun)
- CORS support with configurable options
- Delay simulation for network latency testing
- Read-only mode for safe demonstrations
- Static file serving for frontend assets
- Custom middleware support for advanced use cases
- Deno compatibility for modern runtime support
- Beautiful CLI interface with color-coded outputs and intuitive feedback
- Comprehensive pagination with flexible query parameters
- Advanced filtering and sorting capabilities
With API prefix enabled, you can also access the same data at http://localhost:3000/api/posts/1.
$3
`typescript
import { create } from '@webmasterdevlin/json-server';
// Create a server with custom options
const server = create({
port: 3001,
host: 'localhost',
delay: 500, // Add 500ms delay to all responses
});
// Load database from file
server.loadDatabase('./db.json');
// Start the server
server.start().then(() => {
console.log('Server is running at http://localhost:3001');
});
`
$3
`typescript
// Import from URL or local file
import { create } from 'npm:@webmasterdevlin/json-server';
// OR use the mod.ts entry point
// import { create } from "./mod.ts";
const server = create({
port: 8000,
host: 'localhost',
});
// Load database from file
server.loadDatabase('./db.json');
// Start the server
await server.start();
`
Routes
All HTTP methods are supported:
` GET /posts
GET /posts/1
POST /posts
PUT /posts/1
PATCH /posts/1
DELETE /posts/1
`
` GET /posts?title=json-server&author=webmasterdevlin
GET /posts?id=1&id=2
`
Pagination and sorting:
` GET /posts?_page=1&_limit=10
GET /posts?_sort=title&_order=asc
GET /posts?_sort=title&_order=desc
`
API Prefix
The API prefix feature allows you to access all your resources with an /api prefix, making your mock API feel more like a production backend. When enabled, both standard routes and API-prefixed routes work simultaneously.
$3
- Production-like experience: Makes your mock API behave like a real backend with /api routes
- Frontend framework compatibility: Works seamlessly with frameworks that expect API routes to start with /api - Route organization: Helps differentiate API routes from static file routes
- Development consistency: Matches common backend API patterns
$3
Enable the API prefix feature using the --enable-api-prefix (or --api shorthand) flag:
One of the standout features of this implementation is the beautifully styled CLI interface, designed to make your development experience more enjoyable and informative.
$3
When you start the server, you'll see a beautiful status banner with:
` š JSON Server is running
http://localhost:3000 - API Root
http://localhost:3000/db - Full Database
Read Only: No
API Prefix: Enabled
CORS: Enabled
ā¹ļø Press Ctrl+C to stop the server
`
$3
All CLI messages are color-coded for better readability:
- š¢ Green for success messages
- šµ Blue for informational messages
- š” Yellow for warnings
- š“ Red for errors
`javascript
// This will work with both your mock server and production API
const response = await fetch('/api/users');
const users = await response.json();
`