Create an AJAX impersonation request using Gulp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.