Coder Social home page Coder Social logo

moky's Introduction

MOKY

moky = mock + proxy

JavaScript Style Guide

NPM version Build Status Downloads

A proxy server with mock

How to use

If you have experience in webpack, it'll be a piece of cake!

Here comes the quick start:

  • Install as a global command, npm i moky -g
  • Create a file named moky.config.js in your project
  • Run moky in the same directory with moky.config.js

...Yeah, it'll not work actually :-) You need edit moky.config.js at first.

Quick start

If you really want to have a try, clone the project to disk

  • cd moky/example
  • Run moky
  • Open http://localhost:3000 & http://localhost:3000/page (default to mock mode)
  • Exit and rerun moky -e dev, refresh two pages (async request is pass to proxy)

Configure

The following is just a example, edit as your ways.

cat moky.config.js

var path = require('path');

module.exports = {
  /* Listen port for moky server, OPTIONAL */
  localPort: 3000,
  /* Show detail log, OPTIONAL(default false) */
  verbose: false,
  /* Auto create mock file if not exists, OPTIONAL */
  autoGenMock: false,
  /* Asnyc api mock data, OPTIONAL */
  asyncMockPath: path.join(__dirname, 'mock'),
  /* Template mock data, OPTIONAL */
  viewsMockPath: path.join(__dirname, 'tplMock'),
  /* Default mock data, OPTIONAL */
  defaultMock: {},
  /* Root directory for template rendering, REQUIRED */
  viewsPath: path.join(__dirname, 'views'),
  /* Path of favicon.ico, OPTIONAL */
  faviconPath: path.join(__dirname, 'public', 'favicon.ico'),
  /* Static router, OPTIONAL but usually required */
  publicPaths: {
    '/css': path.join(__dirname, 'public', 'css'),
    '/js': path.join(__dirname, 'public', 'js'),
  },
  /* Template engine settings, the same as koa-views, REQUIRED */
  viewConfig: {
    extension: 'html',
    map: { html: 'nunjucks' },
  },
  /* Host name of proxy, works for `virtual hosts`, OPTIONAL */
  hostName: 'hacker-news.firebaseio.com',
  /* Settings for proxy, OPTIONAL */
  proxyMaps: {
    dev: 'https://hacker-news.firebaseio.com',
    local: 'http://localhost:8080',
  },
  /* Not show logs if url is in the list, OPTIONAL */
  filteredUrls: [],
  /* Settings for template page routing, REQUIRED */
  urlMaps: {
    '/': 'index',
    '/page': 'page/index',
  },
}

Template engine

The moky has integrated some major template engines, if your template is one of the following type, your don't need to do anything, it works well.

  • freemarker (JAVA_HOME should set correctly)
  • handlebars
  • nunjucks
  • ejs

BUT, for we use koa-views and koa-views use consolidate.js, it's quite easy to enable a template engine, if you use a template engine we don't include, issues welcome :-)

More

moky -h

Usage: moky [options]

Options:
  -c, --config   Configure file path                 [default: "moky.config.js"]
  -e, --env      Debug env, see <proxyMaps> in configure file  [default: "mock"]
  -h, --help     Show help                                             [boolean]
  -V, --verbose  Show detail log
  -n, --new      Auto create mock file if not exists
  -v, --version  Show version number                                   [boolean]

Integrate in your app

It's easy to integrate moky in your own cli app.

  • Install as dependencies: npm i moky -S
  • In the entry of your app:
import path from 'path'
import { moky, parseConfig } from 'moky'

// get path of moky.config.js and env
// you can use commander.js or yargs

const options = parseConfig(path.resolve(config))
options.env = env

moky(options)

For more, see src/cli.js

Tips

  • Be tired with filling common views mock ? Try putting a __COMMON__.js{on} in viewsMockPath !
  • Mock files with js(Should be exported as CommonJS module) & json extension are friendly supported.

License

license

moky's People

Contributors

int64ago avatar

Watchers

James Cloos avatar Sunny 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.