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

blt-node-mock-server

v0.0.2

Published

File based Node API mock server

Downloads

3

Readme

node-mock-server

File based Node API mock server

Build status Build status Dependencies npm npm

node-mock-server-ui.png

Features

  • Node.js and file based
  • API documentation UI
  • Mock functions
  • Faker included
  • Multiple expected responses
  • Error cases
  • Swagger import
    • DTO import
    • DTO response function
  • Mock response validation
  • DTO preview
  • DTO to Class converter

Getting Started

This plugin requires Node ~0.12.7 or higher

brew install node
npm install node-mock-server --save-dev
node app.js

The "app.js" file

Overview

In your project's root, add a file named for example app.js.

var mockServer = require('node-mock-server');
mockServer(options);

Options

options.restPath

Type: String Default value: './rest'

A string value that is used to define the path to the rest API folder.

options.dirName

Type: String

A string value that is used to define the root directory (__dirname).

options.title

Type: String Default value: Api mock server

A string value that is used to define the title of the specification page.

options.version

Type: Number Default value: 1

A number value that is used to define the API version.

options.urlBase

Type: String Default value: http://localhost:3001

A string value that is used to define the API url.

options.urlPath

Type: String Default value: /rest/v1

A string value that is used to define the path under which the API will be available.

options.port

Type: Number Default value: 3001

A number value that is used to define the port.

options.privateKey

Type: String

A string value that is used to define the private key for ssl.

options.certificate

Type: String

A string value that is used to define the certificate for ssl.

options.funcPath

Type: String|Array Optional

A string or array that is used to define where the response functions are located.

options.headers

Type: Object Default value: {}

A object that is used to define the global response headers. Will add the given headers to all responses.

options.contentType

Type: String Default value: application/json

A string that is used to define the header "Content-Type".

options.accessControlExposeHeaders

Type: String Default value: X-Total-Count

A string that is used to define the header "Access-Control-Expose-Headers".

options.accessControlAllowOrigin

Type: String Default value: *

A string that is used to define the header "Access-Control-Allow-Origin".

options.accessControlAllowMethods

Type: String Default value: GET, POST, PUT, OPTIONS, DELETE, PATCH, HEAD

A string that is used to define the header "Access-Control-Allow-Methods".

options.accessControlAllowHeaders

Type: String Default value: origin, x-requested-with, content-type

A string that is used to define the header "Access-Control-Allow-Headers".

options.swaggerImport

Type: Object Optional

A object that is used to define the swagger import.

options.swaggerImport.protocol

Type: String Default value: http

A string that is used to define the protocol for the swagger import curl.

options.swaggerImport.authUser

Type: String Optional

A string that is used to define the basic auth user for the swagger import curl.

options.swaggerImport.authPass

Type: String Optional

A string that is used to define the basic auth password for the swagger import curl.

options.swaggerImport.host

Type: String Required

A string that is used to define the host for the swagger import curl.

options.swaggerImport.port

Type: String Default value: 80

A string that is used to define the port for the swagger import curl.

options.swaggerImport.path

Type: String Default value: ``

A string that is used to define the path for the swagger import curl.

options.swaggerImport.dest

Type: String Required

A string that is used to define the destination path for the swagger import.

options.swaggerImport.replacePathsStr

Type: String Default value: ``

A string that is used to define the part of the swagger imported methods path which should be removed.

options.swaggerImport.createErrorFile

Type: Boolean Default value: true

A boolean that is used to decide to create an expected response error file or not.

options.swaggerImport.createEmptyFile

Type: Boolean Default value: true

A boolean that is used to decide to create an expected response empty file or not.

options.swaggerImport.overwriteExistingDescriptions

Type: Boolean Default value: true

A boolean that is used to decide to replace an old description with the new (imported) description or not.

options.swaggerImport.responseFuncPath

Type: String

A string that is used to define where the imported response functions are located.

Usage Examples

Default Options

var mockServer = require('node-mock-server');
mockServer({});

Custom Options

var mockServer = require('node-mock-server');
mockServer({
	restPath: __dirname + '/mock/rest',
	dirName: __dirname,
    title: 'Api mock server',
    version: 2,
    urlBase: 'http://localhost:3003',
    urlPath: '/rest/v2',
    port: 3003,
    funcPath: __dirname + '/func',
    headers: {
    	'Global-Custom-Header': 'Global-Custom-Header'
    },
    swaggerImport: {
    	protocol: 'http',
    	authUser: undefined,
    	authPass: undefined,
    	host: 'localhost',
    	port: 3001,
    	path: '/src/swagger/swagger-demo-docs.json',
    	dest: dest,
    	replacePathsStr: '/v2/{baseSiteId}',
    	createErrorFile: true,
    	createEmptyFile: true,
    	overwriteExistingDescriptions: true,
    	responseFuncPath: __dirname + '/func-imported'
    }
});

Folder structure

  • example_rest_folder
|- group
|--- #path
|--- #path#{param}
|----- method (GET, POST, DELETE ...)
|------- mock
|--------- success.json
|--------- success.headers.json
|--------- error.json
|--------- error-401.json
|------- desc.json
|------- request_schema.json
|------- response_schema.json

Functions in mock data

Faker in mock data

Query params in mock data

For example call GET "/products/superProductCode/?currentPage=1" Config in mock response Response will be:

{
	"currentPage": 1,
	...
}

Dynamic path params in mock data

For example call GET "/products/superProductCode/?currentPage=2" Config in mock response Response will be:

{
	"productCode": "superProductCode"
	...
}

Expected response

  • Use the ui to configure the expected response for each call
  • Use the get param "_expected"
  • Use the request header "_expected"
  • If an dynamic path param is empty or in placeholder format an "400 bad request" will be the response

Mock response validation

  • In case of you using params (form or get) in mock data, you can simulate them by adding an ".request_data.json" file.

Response Header

  • Use the options.headers object to define global response header
  • Add an *.header.json beside the expected response file

License

MIT License

Changelog

Please see the Releases