In my spare time, I built a Mock Server based on Node.js for my own amusement. Simple function, very, very, very small white level, but can meet the vast majority of needs.
Meow Mock source
The value of
-
Easy to test: Create virtual objects instead of real objects that are uncertain or difficult to construct.
-
Avoid waiting: Front-end and server development schedules are often out of sync. A Mock Server can be used by the front end to impersonate a Mock interface by convention without being constrained by the Server’s schedule.
-
Instead of interface documents: interface files can be divided according to business types, and the document content can be rendered to the foreground for easy reference.
features
-
The request type can be GET or POST. The page + count and start_num + count parameters are optional. Detailed differences will be explained later.
-
Data types support values, booleans, strings, images, rich text, dictionaries, and lists. All data can be generated dynamically, but can still be defined as fixed values.
Start the way
cd meow-mock
npm install
npm run start
Copy the code
The default port number is 8888, which can be changed in the /bin/www file:
var port = normalizePort(process.env.PORT || '8888');
Copy the code
If node. js is not installed, please download and install it here.
If Npm is not installed, perform the following steps:
curl -L https://npmjs.com/install.sh | sh
Copy the code
Method of use
Create a.js interface file, such as example.js, in the /data folder. Open route.js and add the following code:
var example = require('./data/example');
var requestGatherLst = [
example
];
Copy the code
This can be interpreted as registering the interface file you just created with the service. We can create multiple interface files module1.js, module2.jd, module3.js…
Suppose example.js looks like this:
var type = require('.. /type');
module.exports = {
example1: {
url: '/example1/_data'.type: 'GET'.data: function () {
return{}}},example2: {
url: '/example2/_data'.type: 'POST'.data: function () {
return{}}}}Copy the code
Obviously, you need to define the URL and type of each interface, and the return value is the data callback.
Here’s a chestnut:
All data can be generated dynamically
example1: {
url: '/example1/_data'.type: 'GET'.data: function () {
return {
message: 'Request successful'.error: 0.data: {
id: type.id(), / / return id
number: type.number({ // Returns a value
min: 288.max: 999
}),
bool: type.bool(), // Returns a Boolean value
string1: type.string([ // Returns a string
'Copy ONE'.'Copy 2'.'Copy 3'
]),
string2: type.string({ // Returns a string
minL: 5.maxL: 16
}),
image1: type.image([ // return the image link
'http://oij8a9ql4.bkt.clouddn.com/default-fe.jpg'.'http://osm0bpix4.bkt.clouddn.com/thumb.jpg'
]),
image2: type.image({ // return the image link
type: '-thumb'
}),
list1: type.list({ // Return the list
length: 5.data: function () {
return type.number()
}
}),
list2: type.list({ // Return the list
length: 22.index: {
name: 'idx'.format: '0\d'
},
data: function () {
return {
pro1: type.number(),
pro2: type.string()
}
}
})
}
}
}
}
Copy the code
- Id -> returns the ID to avoid reusing the timestamp.
There’s a pit here! Suppose a list is n in length and the list items contain field ids. The time difference between generating each list item is very, very, very small, then:
How can id be repeated… Think of a way to go heavy ~
module.exports = {
timestamps: {},
id: function () {
var _this = this;
var curtime = (new Date()).valueOf();
var recursion = function (key) {
if (_this.timestamps[key]) {
var tmp = recursion(key + 1);
} else {
_this.timestamps[key] = 1;
return key;
}
return tmp;
};
returnrecursion(curtime); }}Copy the code
-
Number -> Returns a value, which can be specified by the min and Max configuration items. The default value ranges from 1 to 11.
-
Bool -> Returns a Boolean value.
-
String1 -> Returns a string of randomly selected values from the configuration list.
-
String2 -> Returns a character string. The value can be specified using the minL and maxL configuration items. The default value is 1 to 11.
-
Image1 -> Returns the image link, which can be randomly selected from the configuration list.
-
Image2 -> returns the image link. The size of the image can be specified using the type configuration item. Currently supported: 640 * 307 (-w), 320 * 320 (-half), 120 * 120 (-thumb), default is -half.
-
List1 -> Returns a list whose length can be specified using the length configuration item. The default length is 0.
-
List2 -> Returns a list whose length can be specified using the length configuration item. The default length is 0.
Open http://localhost:8888/example1/_data to check the return data is as follows:
Since the data is dynamically generated, the results you see may differ from mine
However, before restarting the service, refreshing the browser at the same URL does not affect the returned data, unless parameter values are changed or new parameters are added.
Open http://localhost:8888/example1/_data? A =1 feel it!
To open http://localhost:8888/example1/_data? A =2 feel it!
List type Special description
The list items index
When rendering list data, it is often necessary to render its Ordinal Numbers, such as leaderboards:
The Index configuration item is designed to meet these requirements:
index: {
name: 'idx'.format: '\d'.type: 'int'
}
Copy the code
-
Index. name -> Specifies the name. The default value is index.
-
Format -> Specifies the format, currently: \d, 0\d, 00\d, the default value is \d.
-
Index. type -> Specifies the type of the variable. Currently, int and string are supported. The default value depends on the index format.
About the format
-
\d format: index values are 1, 2, 3, 4… (Default variable type int)
-
0\d format: index value is 01, 02, 03… 10, 11,… (Variable type only string)
-
00\d format: index value is 001, 002… 010, 011,… 099, 100, 101,… (Variable type only string)
The specific effects are as follows:
Note how to write the data configuration item
Recommendation:
list: type.list({
length: 5.data: function() {
return type.number()
}
})
Copy the code
Returns different list values:
Is not recommended:
list: type.list({
length: 5.data: type.number()
})
Copy the code
Return the same list value:
Most of the time we don’t like the results
GET request list data paging
First, the list data request interface object is written:
example2: {
url: '/example2/_data'.type: 'GET'.list_name: 'items'.data: function () {
return {
message: 'Request successful'.error: 0.items: type.list({
length: 36.index: {
name: 'idx'.format: '0\d'
},
data: function () {
return {
id: type.id(),
number: type.number(),
string: type.string(),
image: type.image()
}
}
})
}
}
}
Copy the code
The list_name configuration item determines which field in the returned data is the list to be segmented, and its default value is data.
Two sets of paging parameters
page + count
If the total length of data is 36 and count=10, then:
When page=1, returns the first to the 10th data.
When page=2, data items 11 to 20 are returned.
When page=3, data items 21 to 30 are returned.
When page=4, data items 31 to 36 are returned.
When page > 4, the empty list is returned.
Open http://localhost:8888/example2/_data? Page =1&count=10
start_num + count
Truncate the data from start_num + 1 to start_num + count in the list. If? Start_num =6&count=5;
Open http://localhost:8888/example2/_data? Start_num =6&count=5
Remark:
-
All data is returned by default when there is no paging parameter.
-
If no count parameter is specified, 10 entries are returned by default.
Author: Silly love kitten
My garden: sunmengyuan. Making. IO/garden /
My Github: github.com/sunmengyuan
The original link: sunmengyuan. Making. IO/garden / 2017…