Table of Contents

About

Recognition, this project wouldn't be here with out the great json-server. I thought to myself that JSON was a little verbose. So I created yaml-server so you can have a Mock REST API based on a YAML file instead.

yaml-server is a command line tool that create a REST server based on a YAML file.

Application running

Features

  • Do HTTP requests towards a Mock API using GET, PUT, POST and DELETE created off of a db.yml file.

  • Filter your GET calls with query parameters page and pageSize, example:

    /products?page=1&pageSize=10
    
    1
  • Create new resource, make a POST call with the following format /<new resource>/new, example:

    /kittens/new
    
    1

    Ensure you have a payload as well, looking like for example { title: 'paw paw' }

Install

Either install it globally with:

npm install -g yaml-server
1

OR use NPX

npx yaml-server --port 3000 --database ./db.yml
1

Run

  1. Create a db.yml.

  2. Give db.yml an example content, for example:

    products:
      - id: 1
        name: tomato
      - id: 2
        name: lettuce
    orders:
     - id: 1
       name: order1
     - id: 2
       name: order2
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  3. There are two ways to start:

    1. Quick start, run npx yaml-server, this will start a server on http://localhost:3000 and base it off a db.yml at the project root that you created.
    2. With parameters, You can also configure like so npx yaml-server --port 8000 --database ./db/mydb.yml (If you place db file under ./db/mydb.yml)

See your routes

Open up a browser and navigate to http://localhost:<your port>. Default port is 3000, if you specified port use that as port instead.

The default page at route http://localhost:<port> will tell you what routes and operations are available. Here's a typical response for the default page:

Welcome to YAML Server

Routes available are:

 GET /products
 GET /products/:id
 PUT /products
 DELETE /products/:id

 GET /orders
 GET /orders/:id
 PUT /orders
 DELETE /orders/:id
1
2
3
4
5
6
7
8
9
10
11
12
13

Routes

Routes are created from a YAML file. The default value is db.yml. You can name it whatever you want though.

Routes are first level elements. Consider the following example file:

# db.yml
products:
  - id: 1
    name: tomato
  - id: 2
    name: lettuce
orders:
  - id: 1
    name: order1
  - id: 2
    name: order2
1
2
3
4
5
6
7
8
9
10
11

This will produce routes /products, /orders. Below is a table of supported operations with products as example resource. The same operations are also supports for orders/.

VERB Route Input Output
GET /products None Array
GET /products/:id e.g 3 Object
POST /products object Created object
PUT /products object Updated object
DELETE /products/:id e.g 3 Deleted object