
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
flex-mock-server
Advanced tools
A static server, with easy extendability to become mock server, via flexible configuring of response from file, inline data, function and more.
A static server, with easy extendability to become mock server, via flexible configuring of response from file, inline data, function and more.
npm i -g flex-mock-server
fmsflex-mock-servermock folder and map file path are based on this.cwd, to which the requested file resorts when directory non-exists; if true, then the resorting file path is set to index.cors is implied to be true.npm i -D flex-mock-server
import Server from 'flex-mock-server';
// const Server = require('flex-mock-server').default;
const params = {};
const server = new Server(params);
server.onError((error) => console.error(error));
server.start();
...
server.stop();
Multiple instances could be created with different cwd.
Refer to cli options.
Utility function for before and data handlers to response with default status code behavior.
import { StandardCodeHander } from 'flex-mock-server';
// const StandardCodeHander = require('flex-mock-server').StandardCodeHander;
module.exports = {
'dir/file.html': function customHandler(req, res, passedThroughData, logger){
return StandardCodeHander(req, res, 302, {location: 'http://a.com'}, logger);
}
}
{
pattern: response
...
}
patternA string to construct RexExp for url testing and replacing with file path.
[Notes]
- The
urlbeing matched against is whatever left after removing domain androotdirectory (if supplied). e.g./dir1/dir2/abc.extinhttps://domain.com:123/dir1/dir2/abc.ext- Backslash should be escaped if you hope it take effects in constructed RegExp:
file\\.html
responseAllowed types are:
{Number}: Treated as http code, being handled automatically; returns intermediately.
{Array}: An array, http code along with corresponding data: [301, 'http://abc.com']
{object}: a json of complex type, fields are:
before {function(req, res, logger)}: Execute before processing, to modify request or whatever.
after {function(req, res, responseData, logger)}:
Called after response data is got and before sent.
if binary data, fs.ReadStream is passed instead of file content.
data {*}: (match for any type of METHOD)
don't call respone.end()/write() yourself.
path {string | function}: file path.
$&, $', $1.passThrough {bool}: whether to check left items for more matches and pass this data on. default is false.
get/post/... {*}: method respective version of 'data'. }
{string}: shorthand for string version of path: {path:''}
{function}: shorthand for function version of data: { data: func }
This map is walked through twice. The first time matched "before" handlers are executed. The second time to retrieve response data. However if the first time encounters a standard code handler, e.g. type of config is Number/Array, traversing stops, returns immediately.
Passed in parameter logger in custom handlers is used to output information for debugging when debug flag is turned on. Three methods are supplied.
babel-polyfill v6.26.0 is applied, New es6/7 features could be used in this map file.lib/sample.map.js, contents are:const url = require('url');
module.exports = {
// standard code
'/code/401/file\\.htm': 401,
// standard code with arguments
'/code/301/.*': [301, { url: 'http://abc.def.com' }],
// custom data for any methods.
'/data/null': { data: null },
'/data/empty-string\\.htm': { data: '' },
'/data/string': { data: 'hello world' },
'/data/number': { data: 1 },
'/data/json': { data: { customData: '123' } },
// custom handler returning plain data.
'/data/func/plain': {
data(req, res, logger) {
const parsedUrl = url.parse(req.url, true);
const params = parsedUrl.query;
if (params.id == 1) { // eslint-disable-line eqeqeq
return { success: false };
}
return { success: true };
},
},
// custom handler returning plain promise.
'/data/func/promise': function handle(req, res, logger) {
return new Promise((resolve, reject) => {
resolve({ data: 'abc' });
});
},
// shorthand for { data: func }
'/func/file\\.htm': function handle(req, res, logger) {
return { success: true };
},
// explicit method handler prioritized over 'data'.
'/post/file\\.htm': {
post: { customData: '456' },
},
// pre-processing handler to redirect to another path defined before.
'/before/.*': {
before(req, res, logger) {
req.url = '/data/json';
},
},
// don't pass through to 'compound/.*' by default.
'/compound/no-through': {
data(req, res, data, logger) {
return { value: 0, success: false };
},
},
// pass through to next two items by setting 'passThrough' to true.
'/compound/through/file\\.htm': {
data(req, res, data, logger) {
return { value: 1 };
},
passThrough: true,
},
'/compound/through/.*': {
data(req, res, data, logger) {
data.value++;
return data;
},
passThrough: true,
},
// "after" handler to modify response data
'/compound/.*': {
after(req, res, data, logger) {
if (typeof data === 'string') { // read from file
data = JSON.parse(data);
}
data.success = true;
return data;
},
},
// direct file path mapping
'/path/article/(\\d+)/comment/(\\d+)': {
path: 'article_$2_comment_$1.json',
},
// custom file path mapping
'/path/custom/article/(\\d+)/comment/(\\d+)': {
path(req, res, logger) {
return req.url.replace(
new RegExp('path/custom/article/(\\d+)/comment/(\\d+)'),
'article_$2_comment_$1.json',
);
},
},
// shorthand for { path }
'/path/short/article/(\\d+)/comment/(\\d+)': 'article_$2_comment_$1.json',
};
MIT.
FAQs
A static server, with easy extendability to become mock server, via flexible configuring of response from file, inline data, function and more.
We found that flex-mock-server demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.