# nss-json-server

> JSON Server with other useful mixins

Latest version **2.0.1** (published 2025-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install nss-json-server
pnpm add nss-json-server
yarn add nss-json-server
bun add nss-json-server
```

Provides the command `nss-json-server`.

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-07-21 |
| First published | 2019-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.10 |
| Dependencies | 6 |
| Unpacked size | 64.2 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Nashville Software School |
| Maintainers | nashvillesoftwareschool |
| Keywords | JSON, server, fake, REST, API, prototyping, mock, mocking, test, testing, rest, data, dummy, sandbox, json-server, middleware, auth, authentication, authorization, jwt |

## Links

- npm: https://www.npmjs.com/package/nss-json-server
- Repository: https://github.com/nashville-software-school/nss-json-server
- Homepage: https://github.com/nashville-software-school/nss-json-server#readme
- Issues: https://github.com/nashville-software-school/nss-json-server/issues
- npm.io page: https://npm.io/package/nss-json-server

## Dependencies (6)

- [yargs](https://npm.io/package/yargs.md) ^13.2.4
- [express](https://npm.io/package/express.md) ^4.17.1
- [bcryptjs](https://npm.io/package/bcryptjs.md) ^2.4.3
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [json-server](https://npm.io/package/json-server.md) ^0.16.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.1 (latest) — 2025-07-21
- 2.0.0 — 2025-07-21
- 1.1.0 — 2020-03-29
- 1.0.6 — 2020-03-29
- 1.0.5 — 2019-12-02
- 1.0.4 — 2019-12-01
- 1.0.3 — 2019-12-01
- 1.0.2 — 2019-12-01
- 1.0.1 — 2019-12-01
- 1.0.0 — 2019-12-01

## README

# 🔐 NSS JSON Server

This package will pull in useful mixins created by us and other developers. Documentation and options are simplified for instruction of beginners.

Mixins:

* JWT authentication middleware for **[JSON Server](https://github.com/typicode/json-server)** published by Jeremy Bensimon at **[Json Server with Auth](https://github.com/jeremyben/json-server-auth)**
* Nested resource expansion for multiple levels of related resources

## Getting started

### Package Install

```bash
# NPM
npm i -g nss-json-server

# Yarn
yarn global add nss-json-server
```

Create a `db.json` file with a `users` collection :

```json
{
  "users": []
}
```

### Alias Setup

Open your bash or zsh initialization file and add the following alias.

```sh
alias js="nss-json-server -X 7h -p 5050 -w"
```

### Running your API

Run with following command:

```bash
js db.json
```

_It exposes and works the same for all [JSON Server flags](https://github.com/typicode/json-server#cli-usage)._

## Authentication flow 🔑

JSON Server Auth adds a simple [JWT based](https://jwt.io/) authentication flow.

### Register 👥

- **`POST /register`**

**`email`** and **`password`** are required in the request body :

```http
POST /register
{
  "email": "admina@gmail.com",
  "password": "bestPassw0rd"
}
```

The response contains the JWT access token, user id and username (if exists):

```http
201 Created
{
  "accessToken": "xxx.xxx.xxx",
  "user": {
    "id": 1,
    "username": "xxxxxxxx"
  }
}
```

Any other property can be added to the request body without being validated:

```http
POST /register
{
  "email": "admina@gmail.com",
  "password": "bestPassw0rd",
  "username": "admina",
  "firstname": "Admina",
  "lastname": "Straytor",
  "age": 32
}
```

### Login 🛂

- **`POST /login`**

**`email`** and **`password`** are required:

```http
POST /login
{
  "email": "admina@gmail.com",
  "password": "bestPassw0rd"
}
```

The response contains the JWT access token:

```http
200 OK
{
  "accessToken": "xxx.xxx.xxx",
  "user": {
    "id": 1,
    "username": "xxxxxxxx"
  }
}
```

## Creating Owned Resources

If any resource has been guarded with an ownership level route:

| Permission | Description                                                                                 |
| ---------- | ------------------------------------------------------------------------------------------- |
| 600        | User must own the resource to write or read the resource.                                   |
| 640        | User must own the resource to write the resource. User must be logged to read the resource. |
| 644        | User must own the resource to write the resource. Everyone can read the resource.           |


Then when you make a request with the POST or PUT method, and there is an authorization header, then the `userId` will be automatically added to the request body.

#### Example

```js
fetch("http://localhost:5050/posts", {
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: "Bearer xxxxxx.xx.xxxxx"
    },
    body: JSON.stringify({
        url: "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
        image: null,
        timestamp: Date.now()
    })
})
```

Example response:

```json
{
  "url": "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
  "image": null,
  "timestamp": 1575211182251,
  "userId": 4,
  "id": 8
}
```

## Nested Resource Expansion 🔄

NSS JSON Server enhances JSON Server's `_expand` parameter to support multiple levels of resource embedding.

### Basic Usage

The standard JSON Server `_expand` parameter allows you to embed a related resource based on a foreign key:

```
GET /people/1?_expand=city
```

Response:
```json
{
  "id": 1,
  "name": "John Smith",
  "cityId": 4,
  "city": {
    "id": 4,
    "name": "Pittsburgh",
    "stateId": 22
  }
}
```

### Nested Expansion

With the nested expansion enhancement, you can now expand multiple levels deep using dot notation:

```
GET /people/1?_expand=city.state
```

Response:
```json
{
  "id": 1,
  "name": "John Smith",
  "cityId": 4,
  "city": {
    "id": 4,
    "name": "Pittsburgh",
    "stateId": 22,
    "state": {
      "id": 22,
      "name": "Pennsylvania"
    }
  }
}
```

For more details and advanced usage, see the [Nested Expansion Documentation](docs/nested-expansion.md).

## For Local Development

1. Clone repo
1. `npm i`
1. Create a `routes.json` and `db.json`
1. Add the following to your `routes.json`

### Sample routes.json

```json
{
    "/users*": "/640/users$1",
    "/posts*": "/640/posts$1"
}
```

### Starting the Dev Server


1. `npm run build`
1. `node dist/bin.js -w db.json -p 5050 -X 7h -r routes.json`

### Basic Requests

Using Postman, or your favorite HTTP request client, create the following requests.

* http://localhost:5050/register
    ```js
    // Body (raw JSON)
    {
        "email": "admina@gmail.com",
        "password": "Admin8*",
        "name": "Admina Straytor",
        "username": "admin",
        "location": "Nashville, TN",
        "avatar": ""
    }
    ```
    * **Method** - `POST`
    * **Content-Type** header - `application/json`
* http://localhost:5050/posts
    ```js
    // Body (raw JSON)
    {
        "url": "https://media.giphy.com/media/eHWWKfSp0VZ1V87Ixj/giphy.gif",
        "image": null,
        "timestamp": 1575211182251
    }
    ```
    * **Method** - `POST`
    * **Authorization** header - Use token from registration response
    * **Content-Type** header - `application/json`
    * **Accept** header - `application/json`

---
_Source: https://npm.io/package/nss-json-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
