A proxy server with mock
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 withmoky.config.js
...Yeah, it'll not work actually :-) You need edit moky.config.js
at first.
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)
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',
},
}
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 :-)
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]
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
- Be tired with filling common views mock ? Try putting a
__COMMON__.js{on}
inviewsMockPath
! - Mock files with js(Should be exported as CommonJS module) & json extension are friendly supported.