1.2.2 • Published 5 years ago

webpack-devserver-mock-middleware v1.2.2

Weekly downloads
17
License
MIT
Repository
github
Last release
5 years ago

Webpack DevServer Mock Middleware(webpack-devserver-mock-middleware)

Create a directory for mock data

Server would watch the directory and would hot reload whenever whatever changed under the directory.

Create entry file index.js contains configuration similar to the following:

  const config = {
    '/api/user/:id': (req, res, next) => {
      res.json({
        id: req.params.id,
        name: `user-${req.param.id}`
      })
    },
    '/api/users': [{
      id: 1,
      name: 'user-1'
    },{
      id: 2,
      name: 'user-2'
    }],
    '/api/login': request('./login'),// need to create login.js
    '/api/info': request('./info.json'), // need to create info.json
  };
  module.exports = (req, res, next) => {
    const { path } = req;
    const c = config[path];
    if (c) {
      if (typeof c === 'function') {
        return c.call(null,req, res, next);
      } else {
        res.json(c);
      }
    } else {
      next();
    }
  }

Config Webpack DevServer webpackage.config.js:

const path = require('path');
const mockMiddleware = require('webpack-devserver-mock-middleware');
module.exports = {
  devServer: {
    before: function(app, server) {
      // ...
      app.use(mockMiddleware(path.resolve('../mock')));
    },
  },
};
1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago