tianma-ajax-mock

0.8.3 • Public • Published

tianma-ajax-mock

基于天马的AJAX数据模拟插件,用于方便的在开发过程中模拟异步AJAX数据请求。

AJAX从出生时的风光无限,到现在的家喻户晓,基本上大家曾使用到过。 在开发过程中,若页面上有复杂的交互逻辑,一般都会有大量的AJAX请求。 对于前端工程师来说,如果这时后端工程师没有提供好AJAX接口和足够的测试数据来供前端调试, 那么,AJAX接口调试将会是一件极蛋疼的事情。

故基于天马开发此插件,该插件可以通过简单的JSON配置,完成AJAX请求的接口配置和数据模拟。

功能

  1. 自定义AJAX请求参数
  2. 根据AJAX请求参数返回指定JSON数据

安装

利用npm安装到全局

npm install tianma-ajax-mock -g

使用(简单版)

  1. 在tianma目录下

  2. 新建config-ajax-mock.js,例:

     /**
      * @author 周源 <sunny.zhouy@alibaba.com>
      * @update 2013-8-30
      */
     
     require('tianma')
         .play('http://tianma.alif2e.com/conf/icbu-standard-ajax-mock.js', {
             root: './htdocs', // intl-style环境根目录, 默认为 ./htdocs
             port: 80, // HTTP端口,默认为 80
             portssl: 443 // HTTPS端口,默认为 443
         });
    

    或者,打开config.js,在静态mount中添加pipe('tianma-ajax-mock')管道:

     .mount('/', [
         pipe.static({ root: './htdocs' }),
     	pipe.proxy({
         	'http://110.75.216.150/$1': /(?:(?:style|img)\.(?:alibaba|aliexpress)\.com|aliimg\.com)\/(.*)/
         }),
         //////////////////////////
         pipe('tianma-ajax-mock'),
      //////////////////////////
     	pipe.modular({level:'lite', alias:require('./htdocs/unicorn.json').alias, stamp: false })
     ])     
    
  3. 配置ajax-mock-data.js,假如你想发起一个参数为hello=ajaxMock的ajax请求,
    那么你只需要请求这样的URL:

     http://style.alibaba.com/ajax-mock.do?hello=ajaxMock
    

    如果你想返回一个这样的JSON数据:

     {
     	email: "xxx@xxx.xxx",
     	password: "hello1234"
     }
    

    那么你只需要配置这样一个名字为ajax-mock-data.js的文件,例:

     var helloWorld = { // 可配置多组
         //request: {//可删除此项
     	//	type: 'get', // || 'post'
     	//	url: 'style.alibaba.com/ajax.do' //(默认值)
     	//},
     	response: [{ //根据不同的query返回不同的JSON内容
         /**
          * 例如
          */
     		query:"hello=ajaxMock",//请求参数
     		data: {//返回的JSON
     			email: "xxx@xxx.xxx",
     			password: "hello1234"
     		}
     	},{
     		query:"hello=f2e",
     		data: {
     			name: "f2er",
     			speak: "haha, f2e is interesting!",
     			skills:{
     				html:'</>body</>',
     				css:'light',
     				javascript:'perfect!'
     			}
     		}
     	}]
     };
     
     module.exports={
         helloWorld: helloWorld
     }
    

使用(详尽版)

version

0.3.1 publish 0.4.1 add jquery-mock 0.5.1 origin problem solved 0.6.1 客户端连接的服务器端接口改为线上

================ License

tianma-ajax-mock is released under the MIT license:

The MIT License

Copyright (c) 2013 zhou yuan <percy30856@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Thanks

Readme

Keywords

none

Package Sidebar

Install

npm i tianma-ajax-mock

Weekly Downloads

0

Version

0.8.3

License

MIT

Last publish

Collaborators

  • sunny.zhouy
  • johnnychq