Coder Social home page Coder Social logo

agreed-server's Introduction

Agreed Server

Build Status

Agreed mock server

agreed-server is a mock server based on agreed. This module provides CLI executable command and 2 programmable interface. If you want to use agreed as mock, you would be better to install agreed-server.

Install

$ npm install agreed-server --save-dev

Basic Usage

Usage as CLI

$ agreed-server --path ./test/agreed.json --port 10101

Usage as programming

const agreedServer = require('agreed-server');

const server = agreedServer({
  path: 'agreed/agreed.json',
  port: 3001,
  static: './static', // serve files from ./static
  staticPrefixPath: '/public',
}).createServer();

Advanced Usage

Usage as Express pure server

const agreedServer = require('agreed-server');

const { app, createServer } = agreedServer({
  path: 'agreed/agreed.json',
  port: 3001,
  static: './static', // serve files from ./static
  staticPrefixPath: '/public',
  middlewares: [
    logger,
    perfTool,
    secureHeaders,
  ],
  defaultRequestHeaders: {
    'x-forwarded-for': 'nginx'
  },
  defaultResponseHeaders: {
    'access-control-allow-origin': '*'
  },
});

app.use(someGoodMiddleware);
app.use((err, req, res, next) => {
  res.statusCode = 500;
  res.send(`Error is occurred : ${err}, you should see log`);
});
const server = createServer(app);

notifier

Usage as notification event

agreed file

module.exports = [
  {
    request: {
      path: '/messages',
      method: 'POST',
      body: {
        message: '{:message}'
      },
      values: {
        message: 'test',
      },
    },
    response: {
      body: {
        result : '{:message}'
      },
      values: {
        message: 'test',
      },
      // add notify property for notification
      notify: {
        body: {
          message: 'message! {:message}'
        }
      }
    },
  },
]
const agreedServer = require('agreed-server');

const { app, createServer, notifier } = agreedServer({
  path: 'agreed/agreed.json',
  port: 3001,
  static: './static', // serve files from ./static
  staticPrefixPath: '/public',
  middlewares: [
    logger,
    perfTool,
    secureHeaders,
  ],
  defaultRequestHeaders: {
    'x-forwarded-for': 'nginx'
  },
  defaultResponseHeaders: {
    'access-control-allow-origin': '*'
  },
});

notifier.on('message', (data) => {
  console.log(data) // { message: 'message! hoge' }
});

app.use(someGoodMiddleware);
app.use((err, req, res, next) => {
  res.statusCode = 500;
  res.send(`Error is occurred : ${err}, you should see log`);
});
const server = createServer(app);

agreed-server's People

Contributors

akito0107 avatar yosuke-furukawa avatar

Watchers

 avatar  avatar  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.