The last article described how to implement an API service using Node

How to build a good API Mock service

Results achieved:

In the development environment, you can add? The isMock =1 parameter is used to perform data mock (without which normal data is accessed) without any impact to the test and production environments

Implementation steps:

  1. Set up the agent through Webpack.

        //webpack.config.js
    
        proxy: {
            '/mock': {
                target: 'mock',
                changeOrigin:true,
                pathRewrite: {
                    '^/mock': ' '}}}Copy the code
  2. Intercepting requests (such as Axios’s own interceptor)

    • Determine the URL parameters (e.g? ismock=1)
    • Determine the current environment (e.gprocess.env.NODE_ENV == 'development')
    • addbaseUrl = /mock
  3. Remove unreachable code in webpack compression (see WebPack configuration table)

The source address