Code Monkey home page Code Monkey logo

node-simulator's Introduction

node-simulator

When APIs slow your front-end development down: record and replay HTTP responses like a boss.

Overview

A showcase of how to use three other node modules:

* replay (slightly forked)
* express
* http-proxy

Providing a way to stub out a back-end by simply running node-replay in record mode and invoking the requests that your application does or will do in the future.

Setup

npm install git+https://github.com/genesyslab/node-simulator.git

How to use

Create a server.js for all your static content (html/css/javascript etc..)

var express = require('simulator').express;

var app = express();
app.use(express.static(__dirname +'/public'));

app.listen(process.env.PORT || 3000);

Set up http-proxy to forward all api requests to your actual server.

var httpProxy = require('simulator').httpProxy

app.all('/api/*', function(req, res) {
    var proxy = new httpProxy.HttpProxy({target:{host:[YOUR_BACKEND_HOST], port:[YOUR_BACKEND_PORT]});
    proxy.proxyRequest(req, res);
});

Run in record mode to start saving responses from the server. Invoke all requests that you would like to be stubbed out.

REPLAY=record node server

Now run your stubbed out server.

node server

You will notice it is not longer making any requests directly to your backend. Instead, the fixtures are loaded.

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.