选项说明
options.debug
Type: Boolean
Default: false
是否开启调试模式,开启调试模式会在控制台输出流程数据
options.root
Type: String
Default: process.cwd()
WEB服务根路径,mock 数据路径 和 模板路径 都必须在此路径下
options.ip
Type: String
Default: '127.0.0.1'
WEB服务监听地址
options.port
Type: Number
Default: 8091
WEB服务监听端口
options.mock
Type: String
Default: './'
mock 数据文件所在路径
options.template
Type: String
Default: ''
模板文件所在路径
options.ignore
Type: String
| Array
Default: ''
需要忽略的路径,路径配置相对于 mock 目录
options.templateRoute
Type: String
Default: '/'
模板文件统一路由地址
example: 配置选项文件 mock.js 为:
var $path = require('path');
module.exports = {
root: $path.resolve(__dirname),
mock: './demo',
template: './page',
templateRoute: '/html'
};
则模板 ./page/entry.pug
对应的访问路径变更为:
http://127.0.0.1:8091/html/entry.html
options.indexPage
Type: String
Default: './node_modules/spore-mock/lib/index.pug'
mock 服务首页页面路径,一般无需配置此选项
options.links
Type: Array
Default: []
链接列表,用于显示到首页
example:
{
links: [
{
href: 'http://{{publicIp}}:8091',
text: 'Mock服务 http://{{publicIp}}:8091'
},
{
href: 'http://{{publicIp}}:8092',
text: '代理服务 http://{{publicIp}}:8092'
}
]
}
options.qrlinks
Type: Array
Default: []
二维码链接列表,用于显示到首页
example:
{
qrlinks: [
{
href: 'http://{{publicIp}}:8091',
text: 'Mock服务 http://{{publicIp}}:8091'
}
]
}
options.statics
Type: Array
Default: []
静态文件路由列表
example:
{
statics: [
// 指定 dist 目录下的文件都是静态路径
// 例如 `./dist/css/index.css` 访问地址为
// `http://127.0.0.1:8091/css/index.css`
'dist',
// 指定静态路径与路由
// 例如 `./src/mock/static/index.css` 访问地址为
// `http://127.0.0.1/assets/index.css`
{
route: 'assets',
path: 'src/mock/static'
}
]
}
options.mockFormat
Type: Function
Default: function(mockData) { return mockData; }
mock 数据渲染页面前统一格式化过滤函数
example:
配置 mock.js 选项文件
const $path = require('path');
module.exports = {
root: $path.resolve(__dirname),
mock: './demo',
template: './page',
mockFormat: function(mockData) {
return {
htmlWebpackPlugin: {
options: {
mock: mockData
}
}
};
}
};
模板数据 ./demo/entry.js
module.exports = {
code: 0,
data: {
title: 'demo'
}
}
模板文件 ./page/entry.pug
- var out = htmlWebpackPlugin.options.mock;
script.
var data = '!{out.data}';
访问 http://127.0.0.1:8091/entry.html?fedebug=json
获得数据:
{
htmlWebpackPlugin: {
options: {
mock: {
code: 0,
data: {
title: 'demo'
}
}
}
}
}
options.render
Type: Array
Default: []
模板解析器,可自定义使用何种模板渲染数据
example:
配置选项 mock.js
const $path = require('path');
module.exports = {
root: $path.resolve(__dirname),
mock: './demo',
template: './page',
render: [{
// 识别文件后缀名
extname: 'txt',
// 配置该文件的解析方式
parse: function(file, data) {
return $fs.readFileSync(file, 'utf8');
}
}]
}
如果模板中有文件为 ./page/demo.txt
可以通过该路径访问:http://127.0.0.1:8091/demo.html
options.proxy
Type: Array
Default: []
反向代理列表,proxy 选项格式参见 http-proxy-middleware
example:
{
proxy: [{
// 反向代理示例
// 到 http://127.0.0.1:8090/proxy/index.php 的请求,都会转发到
// http://m.db.house.qq.com/index.php
// 例: http://127.0.0.1:8090/proxy/index.php?mod=city&act=geocoderbyip
route: '/proxy/index.php',
proxy: {
target: 'http://openapi.house.qq.com',
changeOrigin: true,
logLevel: 'debug',
secure: false
}
}]
}
options.middleware
Type: Array
Default: []
中间件列表,应当提供 express 可以使用的中间件
example:
配置选项 mock.js
var $proxy = require('http-proxy-middleware');
module.exports = {
root: $path.resolve(__dirname),
middleware: [{
// 中间件示例
// 到 http://127.0.0.1:8090/middleware/index.php 的请求,都会转发到
// http://m.db.house.qq.com/index.php
// 例: http://127.0.0.1:8090/middleware/index.php?mod=city&act=geocoderbyip
route: '/middleware/index.php',
handle: $proxy({
target: 'http://openapi.house.qq.com',
changeOrigin: true,
logLevel: 'debug',
secure: false
})
}]
}