New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fakeserver

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fakeserver

A fake server that runs entirely client-side.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

fakeserver

Introduction

fakeserver is a little framework that lets you implement a web server while you really do not: the fake server runs in the browser and no real server is involved. It does this by replacing XMLHTTPRequest with an object that completely handles your requests on the client.

In addition there is a FakeFetch implementation which can be used to create a simple mock version of the new Fetch API by calling its getFetch method. It does override the browser's fetch implementation at this point, just gives you a fake fetch you can use instead.

fakeserver is built on more-router and mock-xhr.

Usage

  import {FakeServer, Response,
          methodNotAllowedHandler, notFoundHandler} from 'fakeserver';

  // when looking up a handler we do it by using
  // last bit of the URL that wasn't handled
  // by the router (the viewName, set by fakeserver, may also be empty),
  // and by request.method
  let keySpec = [
    {
        name: 'name',
        defaultKey: '',
        extract: request => request.viewName,
        fallback: notFoundHandler
    },
    {
        name: 'method',
        defaultKey: 'GET',
        fallback: methodNotAllowedHandler
    }
  ];

  let fakeserver = new FakeServer(keySpec);

  function handler(variables, request) {
      // construct a Response object with a JSON content-type and 200 OK
      // This is the default: you can pass in the status and response headers
      // as extra arguments to the Response constructor
      return new Response(JSON.stringify({animal: animals.id}));
  }

  // register it for a path, and for GET and empty view name by default
  fakeserver.register('animals/{id}', handler);

  // this is how to handle a PUT request:
  // fakeserver.register('animals/{id}', handler, { method: 'PUT'});

  // override default XMLHTTPRequest
  fakeserver.start();

  // now all XMLHTTPRequests are handled by fakeserver

  // go back to the original XMLHTTPRequest, disable fake server again
  fakeserver.stop();

Keywords

FAQs

Package last updated on 20 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc