1.0.8 • Published 3 years ago

auto-server-js v1.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Auto server js

Simulate json server for test

The main purpose of this tool is to emulate in the most detailed way the response of a json api, writing only a few lines of code, but maintaining great flexibility to define responses

Install

Using npm

$ npm i -g auto-server-js

Usage

Define route-module.js file

The library reads the files that its name begins with route and its extension is .js

Examples:

  • route-users.js
  • routeAuth.js
  • route.js

Write route-module.js file

Default of all properties you can define

module.exports = [
  {
    method: "get", // can be: 'post' | 'put' | 'delete'
    path: "/authenticate",
    routeType: 'normal', // can be: 'pagination' | 'file'
    showBody: false,
    showHeaders: false,
    showParams: false,
    showQuery: false,
    showReponse: false,
    responseStatus: 200,
    responseTime: 0,
    responseHeaders: {
      'Content-Type': 'application/json'
    },
    defaultResponseSize: 20, // define how many items sends when routeType is pagination
    paginationNames: {
      page: 'n_pages', // change default paginate query names for request
      limit: 'page_limit'
    },
    itemsPath: 'elements', // path in response object to insert pagination elements
    response: {
      success: true,
      name: 'John',
      user_id: 2
    }, // if routeType is 'file' this field must be: './path/to/file/hello.txt'
  },
];

Execute server

Use single file

$ asjs --port 4000 --file ./routes-users.js

Use files in the current directory and port 4000

$ asjs

Other examples

Simple GET route

GET http://localhost:4000/hello

module.exports = [
  {
    path: '/hello',
    response: {
      // optional response object
      name: 'atenea',
      surname: 'pinky',
      age: 30
    }
  }
];

Simple POST route

POST http://localhost:4000/hello

module.exports = [
  {
    path: '/hello',
    method: 'post',
    response: {
      // optional response object
      success: true
    }
  }
];

Simulate slow api (wait n seconds to respond)

POST http://localhost:4000/slow_route

module.exports = [
  {
    path: '/slow_route',
    method: 'post',
    response: {
      // optional response object
      success: true
    },
    // wait 2 seconds and respond
    responseTime: 2
  }
];

Serve static files

GET http://localhost:4000/images/doge.jpeg

module.exports = [
  {
    path: '/images/doge.jpeg',  
    routeType: 'file',
    response: './path/to/file/doge.jpeg',
    responseContentType: 'image/jpeg',
    responseHeaders: {    
      'Content-Disposition': 'attachment;filename=doge.jpeg;'
    }
  }
];

Simulate pagination route

Request users with pagination query like this:

http://localhost:4000/users?page=1&limit=10

module.exports = [
  {
    path: '/users',  
    routeType: 'pagination',
    itemsPath: 'elements', // path in response object to insert items 
    response: {
      total: 100,
      elements: [
        {
          // take the first object as example
          id: 1,
          username: 'atenea',
          email: 'atenea@mail.com',
        }
      ]
    },
  }
];

Change pagination query names

Default pagination query names are: 'page' and 'limit'

module.exports = [
  {
    path: '/users',  
    routeType: 'pagination',
    itemsPath: 'elements', // path in response object to insert items 
    paginationNames: {
      page: 'p',
      limit: 'size'
    },
    response: {
      total: 100,
      elements: [
        {
          // take the first object as example
          id: 1,
          username: 'atenea',
          email: 'atenea@mail.com',
        }
      ]
    },
  }
];

And you can use: GET http://localhost:4000/users?p=1&size=10

Simulate route params like express

Only write path property like express library to use params

module.exports = [
  {
    path: '/users/:id',  
    showParams: true,
    response: { success: true },  
  }
];

Debug requests

You can use flags to see content in the request

module.exports = [
  {
    path: '/example',  
    response: { success: true },
    showParams: true, // show params in url specified in path: /example/:param
    showQuery: true, // show query fields in url: https://localhost:4000/example?user_id=1&active=false
    showBody: true, // show request body
    showHeaders: true, // show request headers
    showResponse: true, // show response when is send
  }
];

See content and request

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago