You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

webpack-dev-mock

Package Overview
Dependencies
Maintainers
7
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-dev-mock

Webpack devserver middleware mock server

2.0.0
latest
Source
npmnpm
Version published
Weekly downloads
923
-19.6%
Maintainers
7
Weekly downloads
 
Created
Source

mock-dev-server

mock-dev-server 是一个 express 服务的中间件

读取项目目录下的 mock/index.js 文件生成,会配置对应的接口。

示例

// mock/index.js
const foo = require('./foo.json');
const bar = require('./bar');

module.exports = {
  // 同时支持 GET 和 POST
  '/api/users/1': foo,
  '/api/foo/bar': bar(),

  // 支持标准 HTTP
  'GET /api/users': { users: [1, 2] },
  'DELETE /api/users': { users: [1, 2] },

  // 支持自定义函数,API 参考 express4
  'POST /api/users/create': (req, res) => {
    res.end('OK');
  },

  // 支持参数
  'POST /api/users/:id': (req, res) => {
    const { id } = req.params;
    res.send({ id: id });
  },
};

服务代码使用方法

// server.js
const express = require('express');

const mockServer = require('mock-server');

const app = express();

mockServer(app);

app.get('/', (req, res) => res.send('hello world'));

app.listen(6001, () => {
  console.log('Example app listening on port 6001!');
  console.log('http://127.0.0.1:6001');
});

FAQs

Package last updated on 09 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts