Implementation method using Mock.js random data and using Express output JSON interface

The front-end project uses the backend interface, but when the interface interface is not written, this time can be used with Mock.js first randomly generate some fake data to debug the page

Install Mock. JS

First use Express to create a NodeJS web project, name is Demo, this is not said

 YARN Add MockJS   

Const Mock = Require (‘MockJs’) var data = mock.mock ({‘List | 2’: [{‘ID | +1’: 1, ‘Color’: ‘@color (),’ Date ‘:’ @datetime () ‘,’ IMG ‘:’ @Image () ‘,’ url ‘:’ @url (http) ‘,’ email ‘:’ @Email () ‘,’ name ‘: @name (1, 2) “,’ text ‘:’ @Paragraph () ‘} ]}) Console.log (JSON.STRINGYFY (DATA))
   The above random method can be found in the chain of the MockJS document given at the bottom, Mock.random The method of calling, directly add @ @ @ 就 方 
Output

{“List”: [{“ID “: 1,” color “:” # 8179f2 “,” date “:” 2015-06-22 12:10:08 “,” IMG “:” http://dummyimage.com/250×250 “,”URL”: “http://hwujcvh.fk/vfrjfmi”, “email”: “y.ahbatuek@mbkhfybrh.pl”, “name”: “James Ronald Rodriguez”, “Text”: “ZSOGSHTW QjscoE QWGGNFK PPBDPQD AVFTD pvczrvnu gsyfyefm rbnbjyy tgemy buple ieghyjp klcxauofu lhjmnb kjpyodk. Njync ysrvx jevei stawy mcosrlpo iacryqob wkkcfuh nkrrdutr zduikjvtf dcv pppbhi ygjnkmg xvpusp ayu lvu. Wgqmtwvo ibqzp cct kodyh ovz slo cpc uqaseuwv ubjgbf hihh oudly mooztiojpi tubmwhsmb kktbkyqp hsvwgoluu jrkosqudm. Wpumdvtw riytwoa sbittrr xtjy beorkb osnjpigntu ndrgxhozeq Iufhu HPUIRGMH LSTOIJPQX Hopk Lkxceqhvr UYMJ PGDMS NJJMU IVXIJMOKN. “}, {” ID “: 2,” Color “:” # 94f279 “,” date “:” 1980-02-20 19:46:44 “,” img “: “http://dummyimage.com/336×280”, “URL”: “http://voyqj.cx/jjyksqzur”, “email”: “k.ydgui@gixl.cr”, “name”: “Ronald Nancy Harris “,”text “:”. Lbdpwqwpgd sodipqu oncnnyis ebtwho dnbt fqxnjyzr qtrriop gfbjt prto dgmtgff gwaqnhon sdlvpxpj pqomfflsc skj Cvteunoj oqmjnfm vowvmw ypywtr klcazkvg cvsyzayytl bgvywe kfqqzhfg iahm jwury xsgf xnr pvfxwhaed nauookwi xuxtgnwq flcbmnrm qglgziy vegml Cexit vdotefuj nptmei hekmljnt bukxrd ndhj lkfyjcv oldpgo rrj kprfklt nfks yvrvc. . Aynbyd hxguza ftjrn kmlirqo wxald jqjkvahim jnhezpgm usev qbynwhm yotehgkwdg eyxj vfnm icchnds dgmd odxajing vqrdl yhpp eba. Tykxnhe njod bslwbsjcj rwlv rkvxk rypew fpfqrqi psislxuwgs jcwrbtfn qeszy leovhc jwupwzo kitct nhbdhjq xyiajxms. Gfgkw nnlg drcqnpwn bowqknwy oiw yysaohk fqqsbgvp mulik vusikwv nbp kpbo nhti dhf hrgql
  Integrated into Express Output JSON  

const mock = Require (‘Mockjs’) Exports.indEX = function (REQ, RES) {var data = mock.mock ({‘List | 2’: [{‘ID | +1’: 1, ‘color’: ‘@color (),’ Date ‘:’ @Datetime () ‘,’ img ‘:’ @Image () ‘,’ url ‘:’ @url (http) ‘,’ email ‘:’ @Email () ‘,’ name ‘: @Name (1, 2) “, ‘Text’: ‘@paragraph ()’}]} // Delay 1 second, analog network request time settimeout (function () {res.send (json.stringify (data))}, 1000)

Express cross-domain
   The interface address will definitely not the same, at this time, the request interface will be involved. To cross-domain, the solution in Express is as follows 

App.all (‘*’, Function (Req, Res, NEXT) {RES.HEADER (“Access -Control-allow-Origin, “http: // localhost: 8080”); Res. Header (“Access-Control-HEADERS”, “X-Requested-with”); Res. Header (“Access-Control -Allow-Methods “,” PUT, POST, GET, DELETE, OPTIONS “); Res. Header (” X-Powered-by “, ‘3.2.1’) ​​Res. Header (” Content-Type “,” ApplicatioN / json; charset = utf-8 “); res.header (‘access-constral (‘ Access-Control-Allow-Credentials’, true); Next ();});

Description: The above code is found online, but there is no such thing in the Internet. Res. Header (‘Access-Control-Allow-Credentials’;
 My front-end project is http: / / localhost: 8080 So the value of Access-Control-Allow-Origin is http: // localhost: 8080   can be modified according to its own server 
Reference

Summary

The above is the use of Mock.js random data and usage for the small series. Express outputs the implementation method of the JSON interface, I hope that everyone can help, if you have any questions, please leave a message, the small package will reply to everyone in time!


© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment