Mock simulation data and fetch pits encountered by interface calls

Source: Internet
Author: User
Tags json require

Before the mock inside with Axios encountered pit, and then the project with a fetch, sure enough I met the pit, write a small note.

Because the interface of the project is not available, but the interface request exists, to use a mock intercept, let the requested data jump directly to the mock.

Mock file configuration:

Use mock const mock = require (' mockjs ');
Let test = Mock. Mock ({' list|1-100 ': [{' id|+1 ' £ 1,////Ordinal attribute value is automatically added 1, initial value is 1 ' businesscode ':/\d {1,10}/,//merchant ID ' proversion|1 ': [' Standard Edition] ', ' Enterprise Edition ', ' Trial Version '],//product version randomly selected 1 elements ' Storecode ':/\d {1,10}/,//Store code ' storename ': ' @cname ',//Store name ' status|1 ': [' Try Use ', ' use ', ' continue '],//state randomly select 1 elements ' effectdate ': ' @DATETIME ("Yyyy-mm-dd HH:mm:ss") ',//Effective Date}], "page": 1, "PageSize": 5 , "totalrecords": 100,});

Module. Exports = {[' Get/api/recharge '] (req, res) {Res. Status (+). JSON (test);},};


Roadhogrc.mock.js configuration file: Const FS = require (' FS '); Const PATH = require (' path '); Const MOCKPATH = path. Join (__dirname + '/mock ');
Const mock = {}; Fs. Readdirsync (Mockpath). ForEach (file = {
Object. Assign (mock, require ('./mock/' + file)); });
Module. exports = mock;


Service.js File configuration:

Import {     stringify} from ' Qs ', import {     getserviceurl} from '. /.. /common/config '; Import request,{Requestget} from '. /.. /utils/request '; The
//configuration Interface address, is judged, if it is an online environment, it is to put the back of the urms, if not will be using the mock data//Const DEV = Process.env.NODE_ENV = = ' production '? False: True
//Export function Fetch (params) {// const Apiurl = '/api/recharge ';// const url = Dev = = = true? apiur L:getserviceurl (Apiurl, ' urms ');  console.log (URL)// return request (URL, ' ${stringify (params)} '); }
Export function Fetch (params) {     return requestget ('/api/recharge ', ' ${stringify (PA Rams)} '); }
//The original interface address//export function Exportout (params) {// return request (Getserviceurl ('/recharge/exportout.actio n ', ' urms '), ' ${stringify (params} ', ' blob ', ' commodity information. xls '); // }


I have a problem in this, is that the configuration of the mock inside the path to be consistent with the service. Otherwise it will newspapers the port error, as well as cross-domain issues, because not one end product will certainly encounter this cross-domain problem. This is one of the solutions.
Note that I injected {requestget} into the service.js because there is an encapsulated file at the bottom, and then you can call the Get method directly.
There is a green note for the purpose of judging whether it is an online environment, if it is an online environment, the system will be directly added to the field, if it is a test environment, just drop the data in the mock directly. Also note that the request here is the method of post requests, in the mock inside to change the get to post, to capitalize. Then in the service.js there is no need to inject {request}.


CHI brother configured the address of the pickup, made a judgment, if it is an online environment, it is to put the back of the urms, if not will use the mock data//Const DEV = Process.env.NODE_ENV = = ' production '? False:true;
Export function Fetch (params) {//Const APIURL = '/api/recharge ';//Const URL = Dev = = = true? Apiurl:getserviceu RL (Apiurl, ' urms '); Console.log (URL)//return request (URL, ' ${stringify (params)} '); // }



Module. Exports = {[' Post/api/recharge '] (req, res) {Res. Status (+). JSON (test);},};

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.