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

dsg-mock-restful-express

v0.0.3

Published

A RESTful mock server powered by Express.js

Downloads

3

Readme

Build Status

mock-restful-express

A RESTful mock server powered by Express.js

Table of Contents

  1. Purpouse
  2. Usage

Purpouse

The purpose of this project is to provide a generic RESTful API server based on mocked responses, defined programatically and with support to many features like:

  • Basic Auth
  • Cookies
  • HTTP endpoints with support to GET, POST, PUT and DELETE actions

Usage

Project Routes

This project is based on the concepts of Express.js routing. Those features are provided by a library called dynamic-routes, and all routes can be defined programatically with support to all HTTP methods (GET, POST, PUT and DELETE) within the routes folder.

To know better these routes conventions, take a look on the project examples to understand the concepts.

Project Mocks

This project is also based on mocked responses, defined programatically in a convetional manner, within the mocks folder.

Examples

The /examples folder within this project contains all the references explained below. To see them in practice, make the mock-server install through npm, as explained below:

npm install mock-restful-express -g

This command make a installation of this library within node global dependencies folder. To run the mock server, within the examples folder, run the following on the terminal:

mock-server

This should initiate a new instance of the server on the default port (8880).

Mocks path structure

The mocks path structure are the defined based on routes path. For example, suppose we have an /foo route defined on the project, with support to GET action. The respective mocked reponse of this route is structured as follows:

- mocks
|-- foo
|--- get
|---- 1.json
|---- all.json
|---- foo=bar.json

This structure is also adaptive to define mocked responses for POST, PUT and DELETE actions whenever necessary.

Mock response structure

All mock responses also are based on some conventions, defined in a structure as follows:

{
	"code": 200,
	"content" : { 
		"bar" 
	}
}

This structure defines a response HTTP code (with support to all HTTP codes) and payload, represented by content node.

Response for Path Param Request

A GET request on a route path with path param like /foo/1 is conventionally mapped to 1.json file, defined in a structure like shown below:

- mocks
|-- foo
|--- get
|---- 1.json

Response for Query Param Request

A GET request on a route path with query param like /foo/?foo=bar&bar=foo, its convetionally mapped to a foo=bar&bar=foo.json file, defined in a structure like shown below:

- mocks
|-- foo
|--- get
|---- foo=bar&bar=foo.json

Response for Subresource Request

A GET request on a route path defined for a subresource like /foo/1/bar/2, its convetionally mapped to a 1_2.json file, defined in a structure like shown below:

- mocks
|-- foo
|--- bar
|---- get
|---- 1_2.json

Default responses with MockHTTP

All requests received from your project routes could be conventioned to have a defined mocked response. For GET methods like /foo, its expected to have a all.json file defined to response for the request. In case of PUT or DELETE actions, the default response is agreed to be defined with HTTP Code 204 without any body. In case of POST requests, the default response is defined to have HTTP Code 200 without any body.

In case of some route doesn´t have its respective mocked response, a message will be sent with HTTP Code 404 and body with a descriptive error message.

This behavior could be easily supported by your project's routes through a library MockHTTP, defined on this project. To know better this libray usage, take a look at this project example section.