A lightweight JSON Server implementation based on Next.js App Router. Inspired by json-server, it provides a simple RESTful API based on JSON files.
- 💡 Next.js App Router support
- 🚀 Simple setup
- 📝 JSON file based data management
- 🔄 RESTful API support
- 🛠 Customizable endpoints
- 🛢️ PostgreSQL support with Drizzle ORM
- Install the package
npm install next-json-server
- Create a
db.json
in your project root
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postsId": "1" },
{ "id": "2", "text": "another comment about post 1", "postsId": "1" }
],
"users": [{ "id": "1", "name": "yuyakinjo" }]
}
- Generate JSON API route
npx next-json-server generate json
This will create /app/json/[...api]/internal.ts
and /app/json/[...api]/route.ts
files in your project.
- Start your Next.js development server
npm run dev
- Access your API at
http://localhost:3000/json/posts
- Generate PostgreSQL API route
npx next-json-server generate db/pg
This will create the following files:
-
/app/db/pg/[...api]/internal.ts
- Internal utility functions -
/app/db/pg/[...api]/route.ts
- API route handler -
/app/db/pg/schema/index.ts
- Sample Drizzle schema file
- Configure your PostgreSQL connection in
.env.local
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
-
Customize your schema in
/app/db/pg/schema/index.ts
as needed -
Start your Next.js development server
npm run dev
-
The server will automatically run migrations and seed data on startup
-
Access your API at
http://localhost:3000/db/pg/users
(or any other table name defined in your schema)
- Clone the repository
git clone https://github.com/yuyakinjo/next-json-server.git
- Install dependencies
cd next-json-server
bun install
- Start the development server
bun dev
The following RESTful API endpoints are available:
-
GET /json/posts
- Get all posts -
GET /json/posts/1
- Get post with ID:1 -
GET /json/posts/1/comments
- Get comments for post with ID:1 -
GET /json/posts/1/comments/1
- Get comment with ID:1 for post with ID:1
-
GET /json/posts?id=1
- Get post with ID:1 -
GET /json/posts?id=1&id=2
- Get posts with ID:1 and ID:2 -
GET /json/posts?title=starwars
- Get posts with title "starwars" -
GET /json/posts?gt_views=100
- Get posts with views greater than 100 -
GET /json/posts?lt_views=100
- Get posts with views less than 100 -
GET /json/posts?gte_views=100
- Get posts with views greater than or equal to 100 -
GET /json/posts?lte_views=100
- Get posts with views less than or equal to 100 -
GET /json/posts?ne_views=100
- Get posts with views not equal to 100 -
GET /json/posts?in_views=100,200
- Get posts with views equal to 100 or 200
-
POST /json/posts
- Create a new post
{
"title": "New post",
"views": 0
}
-
PUT /json/posts/1
- Update post with ID:1
{
"title": "Updated post",
"views": 150
}
-
DELETE /json/posts/1
- Delete post with ID:1
The following RESTful API endpoints are available when using the PostgreSQL integration:
-
GET /db/pg/users
- Get all users -
GET /db/pg/users/1
- Get user with ID:1 -
GET /db/pg/posts
- Get all posts -
GET /db/pg/posts/1
- Get post with ID:1
-
POST /db/pg/users
- Create a new user
{
"name": "New User",
"email": "user@example.com"
}
-
PUT /db/pg/users/1
- Update user with ID:1
{
"name": "Updated User",
"email": "updated@example.com"
}
-
DELETE /db/pg/users/1
- Delete user with ID:1
-
GET /db/pg/users?limit=10&offset=0
- Paginate users -
GET /db/pg/users?name=John
- Filter users by name -
GET /db/pg/posts?authorId=1
- Get posts with authorId:1
[
{
"id": "1",
"title": "a title",
"views": 100
},
{
"id": "2",
"title": "another title",
"views": 200
}
]
-
200
- Request successful -
201
- Resource created successfully -
204
- Resource deleted successfully -
404
- Resource not found
-
npx next-json-server generate json
- Generate JSON API route files -
npx next-json-server generate db/pg
- Generate PostgreSQL API route files (with Drizzle ORM) -
npx next-json-server help
- Show help message
- Clone the repository
git clone https://github.com/yuyakinjo/next-json-server.git
- Install dependencies
cd next-json-server
bun install
- Start the development server
bun dev
You can also run it using Docker:
docker compose up -d
Released under the MIT License. See LICENSE for details.