Skip to content

viscomd/http-request-middleware

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-request-middleware

NodeJS http request handling middleware with admin UI.

Is your back-end api not ready, too slow, or unstable? You don't have to wait for that by mocking all back-end responses using http-request-middleware.

Compatible with connect, express, browser-sync and many more.

image

TL;DR

Add the middleware to your code before you start the server.

var express = require('express');
var httpRequestMiddleware = require('http-request-middleware');

var app = express();
app.use(httpRequestMidleware.middlewares);
app.listen(3000);

Install

$ npm install --save-dev http-request-middleware

Getting Started

1. Create mock responses and list to custom-urls.json

{
  "/api/foo": {
    "responses": [ 
      {"name": "case 1", "url": "api-responses/foo.json", "active": true},
      {"name": "case 2", "url": "api-responses/foo2.json"},
      {"name": "case 3", "url": "api-responses/foo3.json"}
    ]
  },
  "/api/bar": {
    "responses": [ 
      {"name": "case 1", "url": "api-responses/bar.json", "active": true},
      {"name": "case 2", "url": "api-responses/bar2.json"},
      {"name": "case 3", "url": "api-responses/bar3.json"}
    ]
  }
}

2. Optionally, create proxy-urls.json to use proxy features.

[
  {
    "active":true,
    "context": [
      "api/foo"
    ],
    "options": {
      "logLevel": "debug",
      "target": "https://www.yourServer.com",
      "secure": false,
      "autoRewrite": true,
      "changeOrigin": true
    }
  }
]

proxy-url options can be found here

3. Update package.json with the configuration files

{
  "name": "my package",
  "version": "0.1.0",
  ...
  httpRequestMiddleware: {
    "basePath": "./dist",
    "customUrls": "./custom-urls.json",
    "proxyUrls": "./proxy-urls.json
  }
}

4. Start your development server with http-request-middleware

browser-sync example

var browserSync = require('browser-sync');
var httpRequestMiddleware = require('http-request-middleware');

browserSync.init({
  logLevel: 'debug',
  host: 'localhost.rogers.com', // e.g. localhost.mysite.com
  port: 3000,
  startPath: '/',
  ui:  { port:  3001 },
  open: 'external',
  browser: 'default',
  server: {
    baseDir: __dirname,
    middleware: httpRequestMiddleware.middlewares
  },
  https: true
});

5. Visit admin UI /developer.html to manage custom urls and/or proxy urls.

https://localhost:3000/developer.html

Configuration at package.json

Example

{
  "name": "eclipse",
  "version": "0.2.7",
  ...
  "httpRequestMiddleware": {
    "basePath": "builds/dist",
    "customUrls": "gulp-tasks/browser-sync/custom-urls.json",
    "proxyUrls": "gulp-tasks/browser-sync/proxy-urls.json",
    "headUrls": [
      "https://fonts.googleapis.com/icon?family=Material+Icons",
      "https://unpkg.com/font-awesome@4.7.0/css/font-awesome.css",
      "https://unpkg.com/mce/dist/themes/blue.css",
      "https://unpkg.com/mce/dist/mce.min.css",
      "https://unpkg.com/mce/dist/mce.min.js"
    ],
    "navigationlinks": [
      "<mce-nav-item href='https://example.com/my-document' icon='fa-book'>Documents</mce-nav-item>",
      "<mce-nav-item href='/web/consumer/developer' icon='link'>Links</mce-nav-item>"
    ]
  }
}
Option Description
basePath the base path that .json files are located. e.g. 'builds/dist'
customUrls the file path of custom urls configuration file .g. './custom-urls.json'
proxyUrls Optional, the file path of proxy urls configuration file .g. './proxy-urls.json'
headUrls Optional, the list of HTML header files
navigationLinks list of html links to be seen on the side bar of admin UI.

Compatible servers

http-request-middleware is compatible with the following servers:

License

The MIT License (MIT)

Copyright (c) 2018 Allen Kim

About

NodeJS http request middleware with admin interface

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 56.1%
  • JavaScript 43.9%