npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fe-mock-server

v0.1.0

Published

just a mock server

Downloads

17

Readme

mock-server

npm version npm downloads

Usage

npm install fe-mock-server -g
fe-mock-server --mock "./config/" --api-prefix "/api" --port 8989
  • --mock: mock files's location
  • --api-prefix: api prefix, default '/'
  • --port: server port, default 8989

About mock configuration file, please check config.

Example

var _ = require('lodash');
// 简单的 RESTful
module.exports = function(configurations) {
	var users = [
		{id: 1, name: 'hurry', grade: 90},
		{id: 2, name: 'hjzheng', grade: 88},
		{id: 3, name: 'Jack', grade: 30},
		{id: 4, name: 'Tom', grade: 70},
		{id: 5, name: 'Bell', grade: 40}
	];

	configurations.add([
		{
			request: {
				method: 'GET',
				urlPattern: '/users'
			},
			response: {
				status: 200,
				body: function() {
					return users;
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		},
		{
			request: {
				method: 'GET',
				urlPattern: '/users/:id'
			},
			response: {
				status: 200,
				body: function(req) {
					return _.find(users,
					{ 'id': parseInt(req.params.id, 10)});
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		},
		{
			request: {
				method: 'DELETE',
				urlPattern: '/users/:id'
			},
			response: {
				status: 200,
				body: function(req) {
					_.remove(users, function(u) {
						return u.id === parseInt(req.params.id, 10);
					});
					return {success: true};
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		},
		{
			request: {
				method: 'POST',
				urlPattern: '/users'
			},
			response: {
				status: 200,
				body: function(req) {
					req.body.id = users[users.length - 1].id + 1;
					users.push(req.body);
					return {success: true};
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		},
		{
			request: {
				method: 'PUT',
				urlPattern: '/users/:id'
			},
			response: {
				status: 200,
				body: function(req) {
					req.body.id = parseInt(req.params.id, 10);
					var index = _.findIndex(users,
					    { 'id': parseInt(req.params.id, 10)})
					users[index] = req.body;
					return {success: true};
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		}
	]);
}

数据配置模式简单说明

通过数据,简化mock配置过程

1. mock简单模式,模块文件导出一个对象

  • 针对get请求,可以直接按照[url]: data的形式配置
// data.js

module.exports = {
	'/test': {
		name: 'aa',
		age: 'bb',
		gender: 'cc'
	},
	'/test2': 'This is data',
	'/test3': [1, 2, 3, '我是一条锦鲤']
}
  • 针对非get请求(支持get/post/delete/put),按照[url]: { [method]: data }的形式配置
// data.js

module.exports = {
	'/test':{
		post: {
			message: 'message from post'
		},
		delete: {},
		get: 'lalal'
	}
}
  • 针对高级请求,需要对request和response做处理的,按照 [url]: (req, res) => {}的形式配置
// data.js

module.exports = {
	'/test': function(req, res) {
		const query = req.query;
		res.status(500);
		return { message: 'This is internal error' }
	},
	'/test2': {
		get(req, res) {
			return req.query;
		},
		post(req, res) {
			return req.params;
		}
	}
}

2. mock兼容模式

在原有的配置方式中,可以兼容简单的配置模式。

module.exports = function (configurations) {
	configurations.add([
		// 获取集团卡列表
		{
			request: {
				method: 'GET',
				urlPattern: '/tt/test'
			},
			response: {
				status: 200,
				body: function () {
					return [];
				},
				headers: {
					'Content-Type': 'application/json'
				}
			}
		},{
			'/test3': {
				msg: '兼容模式'
			},
			'/test33': {
				post: {}
			}
		}
	])
};