A TypeScript implementation of json-server with additional features and comprehensive TypeScript types.
- Full TypeScript support with type definitions
- RESTful API endpoints from a JSON file or JavaScript object
- Configurable routes
- API prefix support (
/api/*
for all routes) - Support for multiple package managers (npm, yarn, pnpm, bun)
- CORS support
- Delay simulation for network latency testing
- Read-only mode
- Static file serving
- Custom middleware support
- Deno compatibility
- Beautiful and intuitive CLI interface with color-coded outputs
npm install @webmasterdevlin/json-server
yarn add @webmasterdevlin/json-server
pnpm add @webmasterdevlin/json-server
bun add @webmasterdevlin/json-server
Create a db.json
file:
{
"posts": [{ "id": 1, "title": "json-server", "author": "webmasterdevlin" }],
"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],
"profile": { "name": "webmasterdevlin" }
}
Start the JSON Server:
npx json-server db.json
Or use it from your package.json scripts:
{
"scripts": {
"mock-api": "json-server db.json"
}
}
Now if you go to http://localhost:3000/posts/1, you'll get:
{ "id": 1, "title": "json-server", "author": "webmasterdevlin" }
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');
});
// 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();
All HTTP methods are supported:
GET /posts
GET /posts/1
POST /posts
PUT /posts/1
PATCH /posts/1
DELETE /posts/1
Create a routes.json
file:
{
"/api/*": "/$1",
"/blog/:id": "/posts/:id",
"/blog/:category/:title": "/posts?category=:category&title=:title"
}
Start the server with custom routes:
json-server db.json --routes routes.json
Now you can access your resources with:
/api/posts
/api/posts/1
/blog/1
/blog/javascript/typescript-basics
Usage:
json-server [options] <source>
Options:
--port, -p Set port [default: 3000]
--host, -H Set host [default: "localhost"]
--watch, -w Watch for changes [default: false]
--routes, -r Path to routes file [string]
--middlewares, -m Path to middlewares files [array]
--static, -s Path to static files [array]
--read-only, --ro Allow only GET requests [default: false]
--no-cors, --nc Disable CORS [default: false]
--no-gzip, --ng Disable GZIP compression [default: false]
--enable-api-prefix, --api Enable /api/* prefix [default: false]
--delay, -d Add delay to responses (ms) [number]
--id, -i Set database id field [default: "id"]
--foreignKeySuffix Set foreign key suffix [default: "_id"]
--quiet, -q Suppress log messages [default: false]
--help, -h Show help [boolean]
--version, -v Show version [boolean]
Use query parameters for filtering:
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
The API prefix feature allows you to access all your resources with an /api
prefix. This is useful when:
- You want to make your mock API feel more like a real backend
- You need to differentiate API routes from other routes in your application
- You're working with frontend frameworks that expect API routes to start with
/api
Enable the API prefix feature using the --enable-api-prefix
(or -api
shorthand) flag:
json-server db.json --enable-api-prefix
This allows you to access resources through both standard and API-prefixed routes:
# Standard routes still work
GET /posts
GET /posts/1
# API-prefixed routes also work
GET /api/posts
GET /api/posts/1
import { create } from '@webmasterdevlin/json-server';
const server = create({
port: 3000,
enableApiPrefix: true, // Enable the API prefix feature
});
server.loadDatabase('./db.json');
server.start().then(() => {
console.log('Server running with API prefix support');
});
import { create } from '@webmasterdevlin/json-server';
// Database object
const data = {
posts: [{ id: 1, title: 'json-server', author: 'webmasterdevlin' }],
comments: [{ id: 1, body: 'some comment', postId: 1 }],
};
// Create server
const server = create({
port: 3000,
host: 'localhost',
readOnly: false, // Allow all HTTP methods
delay: 1000, // Add 1s delay to responses
enableApiPrefix: true, // Enable /api/* prefix for all routes
});
// Create a custom route
server.addRoute('/custom-route', 'GET', (req, res) => {
res.json({ message: 'This is a custom route' });
});
// Start server
server.loadDatabase('./db.json');
server.start().then(() => {
console.log('JSON Server is running');
});
One of the standout features of this implementation is the beautifully styled CLI interface, designed to make your development experience more enjoyable and informative.
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
All CLI messages are color-coded for better readability:
- π’ Green for success messages
- π΅ Blue for informational messages
- π‘ Yellow for warnings
- π΄ Red for errors
πΎ Database loaded: db.json (3 collections, 20 items)
π Loaded 4 custom routes from routes.json
π Watching for changes: db.json
The help command (json-server --help
) provides a well-organized and colorful display of all available options:
β‘οΈ @webmasterdevlin/json-server β‘οΈ
A TypeScript-powered REST API mock server
Usage:
json-server [options] <source>
Options:
--port, -p Set port [default: 3000]
--host, -H Set host [default: "localhost"]
# ...more options...
Examples:
json-server db.json
json-server db.json --port 3001
# ...more examples...
Experience the beautiful CLI interface by installing and running json-server:
npm install @webmasterdevlin/json-server
npx json-server db.json
- Node.js (v16.0.0 or higher)
- npm, yarn, pnpm, or bun
# Clone the repository
git clone https://github.com/webmasterdevlin/json-server.git
cd json-server
# Install dependencies
npm install
# Run development server
npm run dev
# Run tests
npm test
# Build for production
npm run build
Please read our Contributing Guide and Code of Conduct before submitting a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
This project is inspired by the original json-server by webmasterdevlin, rewritten in TypeScript with additional features and improvements.