Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-js-server

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-js-server

Mock server for API

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
94
increased by308.7%
Maintainers
1
Weekly downloads
 
Created
Source

npm version

mock-js-server

A mock server that allow to generate responses by JSONs or JSs files

Configure it

The first step is to configure config/routes.json, where we will map our API endpoint routes to JSON/JS files.

{
  "^$": "../config/info.json",
  ".*user/{{userId}}.*": "user/default.js"
}

Mock files

There're multiple options to return a response via this server:

JSON file

This wil return JSON response at it's defined in the JSON file.

{
  "status": "OK"
}

Execute JS file

/*globals req, params, body, query, mock*/
mock = {
  id: Math.floor(Math.random() * 1000),
  userId: params.userId,
};

if (query) {
  if (query.name && query.surname)
    mock.fullname = `${query.name} ${query.surname}`;
}

Mock Object

You must use the @mock key to add contenType or statusCodel ike this:

{
  "@mock": {
    "status": 404,
    "contentType": "text/html",
    "content": "<html><head><title>404</title></head><body>You hit a 404</body></html>"
  }
}

Also you can add a download action to the mock:

{
  "@mock": {
    "status": 200,
    "@download": "./LICENSE"
  }
}

Or add extra headers:

{
  "@mock": {
    "status": 200,
    "headers": ["Access-Control-Allow-Origin: *"]
  }
}

Or an option, to delay the response:

{
  "@mock": {
    "status": 200,
    "delay": 1200
  }
}

Run it

Just run npm start to run it simple or configure as tu want with these options:

OptionKeyDefault Description
Config file-cconfig.jsonName of configuration file
Config Folder--conf-folder ./config/Path of configuration's folder
Routes File--routes-fileroutes.jsonName of the routes file
Mock Folder--mocks-folder./mocks/Path of the mock folder
Port-pNoneListening port
Delay-dNoneDelay in ms
Headers-HNoneExtra Headers

Keywords

FAQs

Package last updated on 17 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc