Coder Social home page Coder Social logo

sw-proxy's Introduction

sw-proxy

sw-proxy is a really tiny proxy server which utilizes a service worker. A service worker sits between web applications and the network (when available).

Service workers are able to intercept requests and take an action, like serving a custom response.

This enables you to use sw-proxy as a proxy server inside your browser or as a mock server to test a REST API without having to run a local backend.

Installation

Run npm install @dannymoerkerke/sw-proxy --save-dev

Usage

Simply add sw-proxy to your web app using a script tag:

<script src="node_modules/sw-proxy/index.js></script>

Then list the requests you want to intercept in an array of objects and save it to sw-proxy-responses.js in the root folder of your application.

For example:

const responses = [
    {
         url: 'http://api.example.com/json',
         headers: {
             'Content-Type': 'application/json'
         },
         body: {
             message: 'a json response'
         },
         status: 200,
         statusText: 'OK'
    },
    {
        ...
    }
]
  • url: String (required), fully qualified URL of the request to be proxied
  • method: String (required), HTTP method (GET, POST, PUT or DELETE)
  • headers: Object (optional), key/value pairs of HTTP headers
  • body: Object/String (optional): response body
  • file: String (optional), path to file to be served, relative to root folder of the application. Must be accessible to the web server and will be ignored if body is present
  • status: Number (optional), HTTP status code
  • statusText: String (optional), HTTP status text

Running the demo

Run npm install.

Rename sw-proxy-responses.example.js to sw-proxy-responses.js.

Run npm start.

The demo can now be viewed on http://localhost:8080/demo.

Click the buttons on the demo page to generate the requests and view the results in the dev tools of your browser of choice.

To view the console logs in Safari you need to open a separate Web Inspector window via Develop > Service Workers > localhost.

Browser support

Tested in:

  • Chrome 67+
  • Safari 11.1+
  • Firefox 60+
  • Edge 17+

sw-proxy should run in all browsers that support Service Worker.

Inspecting and debugging the service worker

For a great explanation of how to inspect and debug service workers in Chrome, Firefox, Safari and Edge check Tools for PWA developers.

sw-proxy's People

Contributors

dannymoerkerke avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.