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

@kpajtasev/mock-data-server

v2.8.1

Published

Node module for serving mock data

Downloads

38

Readme

mock-data-server

Small NodeJS server for mocked data. When run, it uses data from json or js files for response. It resolves data based on url path. If path does not exist throws 404.

Usage

let server = require('@kpajtasev/mock-data-server');
server.run(`mocksLocation`);

Runs mocking server in default port 9000.

Setting custom port

let server = require('@kpajtasev/mock-data-server');
server.setPort(3000);
server.run(`mocksLocation`);

Cross origin

To fix cross origin errors, setAccessOrigin method can be used. Following code allows requests from local port 9090.

let server = require('@kpajtasev/mock-data-server');
server.setAccessOrigin('http://127.0.0.1:9090');
server.run(`mocksLocation`);

Mocks namespace

Namespace of mock files can be set using setNamespace method. Bellow example means that before resolving file, /api will be removed from path.

let server = require('@kpajtasev/mock-data-server');
server.setNamespace('/api');
server.run(`mocksLocation`);

Filtering

If your mock is array, you can use filtering by sending query parameters.

Example

// test.json
[
  { "test": 2, "name": "J"},
  { "test": 1, "name": "A"},
  { "test": 2, "name": "W"},
  { "test": 3, "name": "E"}
]

When /test is returned full test.json content is returned.

If /test?test=1 is requested, response is:

// test.json
[
  { "test": 1, "name": "A"}
]

Function

Mock also can be defined as function where result of function will be sent as response.

Example

// test.js
module.exports = () => [
  { "test": 2, "name": "J"}
]

When /test is requested response is:

[
  { "test": 2, "name": "J"}
]

Mock Example

let server = require('@kpajtasev/mock-data-server');
server.run(`mocksLocation`);

When http://127.0.0.1:9000/test is request, response will match content of mocksLocation/test file.