dev-api-server
Configurable API server for client development
Last updated 2 years ago by ailrun .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install dev-api-server 
SYNC missed versions from official npm registry.

dev-api-server

Configurable api server (only) for client development

DO NOT USE THIS FOR ANY KINDS OF PRODUCTION LEVEL DEPLOYMENTS

How to Install

  1. To install globally, run following command at any directory.
    npm i -g dev-api-server
    
  2. To install locally, run following command in your project.
    npm i -D dev-api-server
    

How to Use

Let's suppose that you have dev-api.json in your current directory with following contents (You can find the file in examples/dev-api.json)

{
  "apis": [
    {
      "apiPath": "/",
      "body": "Hello dev-api-server!"
    },
    {
      "apiPath": "/file",
      "filePath": "dev-api.json"
    }
  ]
}

To execute dev-api-server,

  1. When you installed it globally,
    dev-api-server
    
  2. When you installed it locally,
    npx dev-api-server
    

Now let's open the browser (or any other REST client tools) and access localhost:3210/ and localhost:3210/file!

You can also use custom config file. This time, let's suppose that you have my-api.json in your current directory with following contents (Again, you can find the file in examples/my-api.json)

{
  "apis": [
    {
      "apiPath": "/dev",
      "body": "hello"
    },
    {
      "apiPath": "/(.*)",
      "code": 503
    }
  ]
}

You can run dev-api-server with

  1. When you installed it globally,
    dev-api-server ./my-api.json
    
  2. When you installed it locally,
    npx dev-api-server ./my-api.json
    

Now when you access localhost:3210/dev, and any other paths will give you HTTP 503 error.

DIY Configurations

To make your own configuration, you can use following keys.

apis

The value of "apis" should be an array of apis.

api

api is one of following type with common keys like "apiPath", "headers".

  1. Body API
  2. Serving API
  3. Failing API

api.apiPath [REQUIRED]

"apiPath" is a path for the api. You can use any string that works with path-to-regexp.

api.headers

The value of "headers" is an optional object whose keys are header names and values are header values. For example, with following json,

{
  "apis": [
    {
      "apiPath": "/",
      "headers": {
        "Content-Type": "application/json"
      },
      "body": "{ \"abc\": \"def\" }"
    }
  ]
}

when you access / path it will give you json-parsed "{ \"abc\": \"def\" }".

Body API

Body API is an api with "body" (REQUIRED) whose value is string or object. However, if you want to get the object as JSON format, you should add "Content-Type" header just like what we did in headers section.

Serving API

Serving API is an api with "filePath" (REQUIRED) whose value is string. This API sends a file in the "filePath". When you use relative path for "filePath", it will resolved from the directory where you execute dev-api-server. "filePath" also accepts path-to-regexp parameters. For example,

{
  "apis": [
    {
      "apiPath": "/file/:file",
      "filePath": "./serving/:file"
    }
  ]
}

will serve ./serving/abc.html for path /file/abc.html, and serve ./serving/world/is.js for path /file/world/is.js.

You can try examples/path-to-regexp.json in examples directory with paths like /dev-api.json and /my-api.json

Failing API

Failing API is an api with "code" (REQUIRED) whose value is number. This API always sends that code as a HTTP status code.

port

You can change your port by setting "port". For example,

{
  "port": 5040
}

will give you a server with 5040 port.

host

You can change your host by setting "host".

Options

You can also have few options.

--help

Surprise surprise... With this option, app displays help message.

--watch

When you run dev-api-server with this option, dev-api-server will be restarted whenever your config json file is updated.

--version

Oh, one more creative option! App will give you a version of dev-api-server!

Author

Current Tags

  • 1.1.0                                ...           latest (2 years ago)

2 Versions

  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 2
Dependencies (7)
Dev Dependencies (6)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |