Overview
Before I used gulp to build front-end projects, this node-based and advocating "programming rather than configuring" tools made me fall in love with him. Powerful gulp can do more than just compress and merge js, CSS, it can do more. Today I bring you to use the gulp foreground to create AJAX simulation data.
The problem solved
Development, the backend has not finished the data output, the front end had to write static simulation data.
L data is too long, write the data in the JS file, and then change the URL.
L Some logic complex code, the addition or removal of simulation data to be cautious.
L want to restore the real data as much as possible, either write more code or manually modify the simulation data.
L Special formats, such as IP, random numbers, pictures, addresses, need to be collected.
Pre-preparation
Installing Gulp (Introduction to use)
Background command to install the Gulp plugin:
NPM Install--save-dev gulp-webserver installation Gulp-webserver
NPM Install--save-dev MOCKJS installation Mockjs
Gulpfile.js File Contents
//Introduction of GulpvarGulp = require (' Gulp ')), Mock= Require (' Mockjs '), webserver= Require (' Gulp-webserver ');//Analog DatavarData={ "/school/getstudent":{ "Id|+1": 1, "Array|1": ["Zhang San", "John Doe", "Harry", "Zhao Liu"] }, "/api":{ "Id|+1": 100, "Success|1-2":true, "City|2": { "310000": "Shanghai City", "320000": "Jiangsu Province", "330000": "Zhejiang Province", "340000": "Anhui Province"}}};gulp.task (' Mock ',function() {GULP.SRC (' Market '). Pipe (webserver ({host:' localhost ', Port:8000, Middleware:function(req, res, next) {Res.setheader (' Access-control-allow-origin ', ' * '); Res.setheader (' Content-language ', ' ZH-CN '); Res.setheader (' Content-type ', ' text/html;charset=utf-8 '); Res.end (Json.stringify (Data[req.url])&&Mock.mock (Data[req.url])); Next (); } }));});
It's simply a matter of writing back the data in the gulpfile.js, and we can actually put the data in the file and then require it in, and here's what the request says:
How, is not very strong, completely independent background to create AJAX simulation data, after learning these can go to their official web search for other plug-ins, gulp powerful features believe you will also be happy
Create an AJAX impersonation request using Gulp