Tutorial on using Mock.js to intercept AJAX requests in a Node.js server environment _node.js

Source: Internet
Author: User


1. Install and use mock in node environment


# Install
npm Install MOCKJS
Using
a mock var mock = require (' mockjs ')
var data = Mock.mock (the
  value of the {//property list is an array containing 1 to 10 elements
  ' list|1- [{
    ///property ID is an id|+1, the starting value is 1, each increment of 1
    ' ': 1
  }]}]
//
output result
console.log (json.stringify ( Data, NULL, 4))


2, intercept the AJAX request call
methods are as follows


Mock.mock (Rurl, Rtype?, Template|function (Options))


Method Description:
(1) Rurl: Optional parameters.



Represents a URL that needs to be blocked, either a URL string or a URL regular. such as/\/domain\/list\.json/, '/domian/list.json '.
(2) Rtype: Optional parameters.



Represents the type of Ajax request that needs to be blocked. such as GET, POST, put, DELETE, and so on.
(3) Template|function: Required parameters, only one of the items.



(4) template represents a data template, which can be an object or a string. For example {' data|1-10 ': [{}]}, ' @EMAIL '.
(5) function points to the AJAX option set for this request, containing the URL, type, and body three properties, see the XMLHttpRequest specification.
Tips



Starting with 1.0, mock.js blocks AJAX requests by overwriting and simulating native XMLHttpRequest, and no longer relies on Third-party Ajax ToolPak (such as JQuery, Zepto, etc.).



3. Intercept Ajax Request Timeout
Configure the behavior when blocking an Ajax request. The supported configuration items are: timeout.



(1) Mock.setup (settings)
(2) settings
Must be selected.
A collection of configuration items.
(3) Timeout
Optional.
Specifies the response time, in milliseconds, of the blocked Ajax request. The value can be a positive integer, such as 400, which means that the response content is returned after 400 milliseconds, or it can be a cross-style string, such as ' 200-600 ', which indicates a response time between 200 and 600 milliseconds. The default value is ' 10-100 '.



4. I understand the interception
using the same method name, go to the column section to specify the method. Modify this point by call to reach intercept.


Implementation principle
//Definition parent class
var mock_ajax = function (str) {
 this.showname=function () {
 console.log (str);
 }
 return this;
};
Define Subclass
var jquery_ajax = function (str) {
 this.showname = function () {
 console.log (' Ajax ');
 }
 return this;
};

Jquery_ajax ("). ShowName ();//-> Ajax

//change this point
to Mock_ajax.call (Jquery_ajax, '");
Call
Jquery_ajax.showname ();




Related Article

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.