{"id":42299,"library":"webpack-dev-mock","title":"mock-dev-server","description":"mock-dev-server is an Express middleware that automatically generates mock API endpoints from a project's `mock/index.js` configuration file. Current stable version is 2.0.0. It supports both simple JSON responses and custom handler functions with full Express 4 semantics, including route parameters. Key differentiators include zero configuration (just place a mock file), support for all HTTP methods, and seamless integration with any Express-based dev server, including webpack-dev-server. It is part of the Alibaba ICE ecosystem but can be used standalone.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/alibaba/ice/tree/master/packages/webpack-dev-mock","tags":["javascript"],"install":[{"cmd":"npm install webpack-dev-mock","lang":"bash","label":"npm"},{"cmd":"yarn add webpack-dev-mock","lang":"bash","label":"yarn"},{"cmd":"pnpm add webpack-dev-mock","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"mock-dev-server is an Express middleware and requires express to function.","package":"express","optional":false}],"imports":[{"note":"Package does not ship ESM; use CommonJS require.","wrong":"import mockServer from 'mock-dev-server';","symbol":"default","correct":"const mockServer = require('mock-dev-server');"},{"note":"The function only accepts an Express app instance; no options object is supported.","wrong":"mockServer(app, options);","symbol":"mockServer (as function)","correct":"mockServer(app);"},{"note":"Without a HTTP method prefix, the route matches both GET and POST.","wrong":"module.exports = { '/api/users': { users: [1,2] } };","symbol":"Mock configuration","correct":"module.exports = { 'GET /api/users': { users: [1,2] } };"}],"quickstart":{"code":"// server.js\nconst express = require('express');\nconst mockServer = require('mock-dev-server');\n\nconst app = express();\nmockServer(app);\n\napp.get('/', (req, res) => res.send('hello world'));\n\napp.listen(6001, () => {\n  console.log('http://127.0.0.1:6001');\n});","lang":"javascript","description":"Shows how to create an Express app and attach the mock middleware, which automatically loads mock routes from mock/index.js."},"warnings":[{"fix":"Ensure `mock/index.js` exists in the directory where `node` is run.","message":"Mock file must be located at `mock/index.js` relative to the project root; otherwise, routes are not loaded.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Install `mock-dev-server` instead of `webpack-dev-mock`.","message":"The package name in npm is `mock-dev-server`, not `webpack-dev-mock` as the repository might suggest.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Use explicit `res.end()` or `res.send()` inside handlers.","message":"The middleware does not support async handler functions; callbacks must be synchronous or use express error handling.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Remove options argument; configuration is done solely via `mock/index.js`.","message":"Version 2.0.0 changed the function signature from `mockServer(app, options)` to `mockServer(app)` only. Options are no longer supported.","severity":"breaking","affected_versions":">=2.0.0 <2.0.0"},{"fix":"Prefix routes with the desired HTTP method, e.g., 'GET /api/users'.","message":"Route definitions without an explicit HTTP method (e.g., '/api/users') match both GET and POST requests, which may cause unexpected behavior.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'2.0.0':30 '4':44 'alibaba':84 'api':17 'automat':14 'base':71 'configur':24,53 'current':26 'custom':38 'dev':3,7,72,77 'differenti':50 'ecosystem':86 'endpoint':18 'express':11,43,70 'express-bas':69 'file':25,58 'full':42 'function':40 'generat':15 'handler':39 'http':62 'ice':85 'includ':46,51,74 'integr':66 'javascript':92 'json':35 'key':49 'method':63 'middlewar':12 'mock':2,6,16,57 'mock-dev-serv':1,5 'mock/index.js':23 'paramet':48 'part':81 'place':55 'project':21 'respons':36 'rout':47 'seamless':65 'semant':45 'server':4,8,73,78 'simpl':34 'stabl':27 'standalon':91 'support':32,59 'use':90 'version':28 'webpack':76 'webpack-dev-serv':75 'zero':52","created_at":"2026-06-04T18:56:35.101622+00:00","updated_at":"2026-06-04T18:56:35.101622+00:00","problems":[{"fix":"Run `npm install mock-dev-server --save-dev` and verify package.json.","cause":"Package not installed or incorrect package name.","error":"Cannot find module 'mock-dev-server'"},{"fix":"Create `mock/index.js` with `module.exports = { 'GET /api/test': { message: 'ok' } };`","cause":"Mock file `mock/index.js` does not exist or is not exporting the correct format.","error":"Middleware not working. Routes not responding."},{"fix":"Change `import mockServer from 'mock-dev-server'` to `const mockServer = require('mock-dev-server');`","cause":"Incorrect import; using ESM import instead of CommonJS require.","error":"TypeError: mockServer is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/alibaba/ice/tree/master/packages/webpack-dev-mock","github":"https://github.com/alibaba/ice/tree/master/packages/webpack-dev-mock","docs":null,"changelog":null,"pypi":null,"npm":"webpack-dev-mock","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing","devops"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-04","next_check":"2026-09-02","install_tag":null}}