npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auto-server-js

v1.0.8

Published

Auto server js

Downloads

3

Readme

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: '[email protected]',
        }
      ]
    },
  }
];

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: '[email protected]',
        }
      ]
    },
  }
];

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