1.0.1 • Published 5 years ago

gulp-cors v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

gulp-cors

功能

会根据mockDir指定的目录(默认为data目录)下的json文件自动生成接口

'host + /test' => response file 'data/test.json'

'host + /test.do' => response file 'data/test.do.json'

支持jsonp

'host + /test.jsonp?callback=cb' => response file 'data/test.jsonp.json'

支持多级目录

'host + /class/test' => response file 'data/class/test.json'

get,post请求支持传参返回不同结果

/data/check.do.js:

module.exports = {
  params: {id: 123},
  response: './check_one.json'
}

or

module.exports = [{
  params: {id: 123},
  response: './check_one.json'
}, {
  params: {id: 456},
  response: './check_two.json'
}, {
  params: {id: 789},
  response: {
    name: "three"
  }
}]

'host + /check.do' => response file 'data/check.do.js'

'host + /check.do?id=123' => response file 'data/check_one.json'

'host + /check.do?id=789' => response content '{"name":"three"}'

configs:

KeyTypeDefaultDescription
paramsObjectNullThe parameters of the meet
delayNumber0Request delay time
routeStringNullRequest url
codeNumber200Status code (Temporary does not support)
responseObject{}The data returned

support powerful command line

Options:

  • -h, --help output usage information
  • -v, --version output the version number
  • -e, --server mockRoot start mock server, such as: mock -e
  • -p, --port port server port config, such as: mock -e -p 8000
  • -f, --fetch url download remote url to local json file, such as: mock -f http://hostname/api/test.do

may be others...

Install

use for gulp

$ npm install --save-dev gulp-cors

use for commandline

$ npm install -g gulp-cors

使用

第一步

新建mock目录 mock

第二步

配置:

var gulp = require('gulp');
var mockServer = require('gulp-cors');

gulp.task('mock', function() {
  gulp.src('.')
    .pipe(mockServer({
      mockDir: 'mock'
      port: 8090,
      allowCrossOrigin: 'http://localhost:8888'
    }));
});

在mock目录下新建test.do.json文件

第三步

访问url => localhost:8090/test.do

选项

KeyTypeDefaultDescription
hostStringlocalhosthostname of the webserver
portNumber8000port of the webserver
pathString/path to the webserver
mockDirString./datamock directory
livereloadBoolean/Objectfalsewhether to use livereload. For advanced options, provide an object. You can use the 'port' property to set a custom live reload port and the filter function to filter out files to watch. The object also needs to set enable property to true (e.g. enable: true) in order to activate the livereload mode. It is off by default.
directoryListingBoolean/Objectfalsewhether to display a directory listing. For advanced options, provide an object with the 'enable' property set to true. You can use the 'path' property to set a custom path or the 'options' property to set custom serve-index options.
fallbackStringundefinedfile to fall back to (relative to webserver root)
openBoolean/Stringfalseopen the localhost server in the browser. By providing a String you can specify the path to open (for complete path, use the complete url http://my-server:8080/public/) .
proxiesArray[]a list of proxy objects. Each proxy object can be specified by {source: '/abc', target: 'http://localhost:8080/abc', options: {headers: {'ABC_HEADER': 'abc'}}}.
allowCrossOriginBooleanfalsewhether to allow cross origin calls (CORS)