You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks json-server, you are great. I gradually created the mockm project in the process of using, maybe it can provide some help for the following problems:
module.exports=(util)=>{return{api: {'get /test/db'(req,res){constdb=global.config._db// Object wrapped by _.chain(db.json)constdata=db.getState()// whole json datadata.user[0].t1=111db.write()// call write to store changesdata.user[0].t2=222db.get(`user`).set(`[0].t3`,333).value()// You can use lodash chain operations, you need to call .value() after the operation is completeres.json(data.user[0])},},db: {user: [{name: `ace`,},],},}}
Intercept requests and responses
Just create the same api and you can intercept it.
module.exports=(util)=>{return{api: {// For example, permission checks can be created at all entry points'use /'(req,res,next){if(req.headers.token===undefined){res.status(403).json({msg: `No permission`})}else{next()}},// Intercepts any requests sent to db.json, and responses from db.josn'patch /books/:id'(req,res,next){// intercept config.dbreq.body.a=`111`// Modify the data passed in by the usernext()res.mm.resHandleJsonApi=async(arg)=>{arg.data.a=`222`// Modify the response, it will not be stored in db.jsonreturnarg.resHandleJsonApi(arg)}},},db: {books: [{id: 1,title: `css`,},],},}}
route rewrite
Acts on static file addresses, addresses generated by db.json, and custom api addresses.
constpath=require(`path`)module.exports=(util)=>{return{api: {async'/upload'(req,res){constmultiparty=awaitutil.toolObj.generate.initPackge(`multiparty`)// or require(`multiparty`)constform=newmultiparty.Form({uploadDir: `${__dirname}/upload/`,})form.parse(req,async(err,fields=[],files)=>{constfile=files.file[0]leturl=`http://127.0.0.1:${global.config.port}/file/${path.parse(file.path).base}`// Can also be saved to db.json: global.config._db.res.json({url})})},},// Support for multiple static directories, and histroy modestatic: {path: `/file`,fileDir: `./upload`,},}}
how to use
Sorry, as I'm not familiar with English, there is currently no documentation in English, but the tool is simple and should be easy to use.
# Check the node version, currently mockm supports node v10.12.0 and above
node -v
# Install
npm i -g mockm
# Run it with the sample configuration
mockm --config
# Browser open http://127.0.0.1:9005/#/apiStudio/
The above command will generate common configuration, modify mm.config.js and try it out.
Thanks json-server, you are great. I gradually created the mockm project in the process of using, maybe it can provide some help for the following problems:
read and write db
Read or change it from custom api or json-server api.
Intercept requests and responses
Just create the same api and you can intercept it.
route rewrite
Acts on static file addresses, addresses generated by db.json, and custom api addresses.
upload files
how to use
Sorry, as I'm not familiar with English, there is currently no documentation in English, but the tool is simple and should be easy to use.
The above command will generate common configuration, modify
mm.config.js
and try it out.The following documents can be helpful to you:
The text was updated successfully, but these errors were encountered: