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

express-mockups-middleware

v1.0.2

Published

Serve mockups on proxified requests

Downloads

7

Readme

express-mockups-middleware

Requirements

You will need Node v6 or later version.

What

This is an express middleware. This middleware purpose is to serve mockups on proxified HTTP requests. This is convenient during development to separate concerns.

Install

npm i --save-dev express-mockups-middleware

Use

You should pass an array of apis as first parameter of the middleware. api objects must respect the following structure.

Property | Required | Type | Description ----------|--------|---------------|------------- pattern | yes | String | A pattern that request.url property should match to be mocked. It will be passed as RegExp argument. enabled | no | Boolean | A boolean that determine if the middleware should handle this api. headers | no | Array of Objects | A array of objects ({key: '', value: ''}) that represents HTTP headers of the mocked response. status | no | Number | A number that will be the HTTP status of the mocked response. body | no | Function | A function that take as parameter the request object and should return a string that will be the message-body of the mocked response.

You can also add an optional configuration object as second parameter.

Parameter | Required | Default value | Description ----------|--------|---------------|------------- enabled | no | true | A boolean that determine if the middleware is enabled or not. force | no | false | Set to true it tells the middleware to ignore enabled property of apis objects. logger | no | empty function | Pass your own logger function that takes a string as parameter.

The middleware call next function of express when no pattern is matched. Otherwise the response is instantly sent to client.

Example

HTTP requests should be proxified otherwise the middleware won't work.

For it you could use http-proxy-middleware.

server.js

import express from 'express'
import proxy from 'http-proxy-middleware'
import mockups from 'express-mockups-middleware'

const app = express()
const resources = ['Resource1','Resource2'];

app.use(mockups([
  {
    pattern: '/resources',
    body: (req) => JSON.stringify(resources)
  },
  {
    pattern: '/resources/(.*)',
    body: (req) => {
      const segments = req.url.split('/')
      return resources[segments[segments.length - 1]]
    }
  }
]));

app.use(proxy('/api', {
  target: `http://distant-backend.com`,
  logLevel: 'debug'
}));

app.listen(8080)

HTTP requests that match one of the patterns listed in apis parameter will be mocked but others will be sent to the proxified backend.