autoresponse
A connect middleware for mocking the http request, can used in edp-webserver
or webpack-dev-server
mocking
Install
Require Node.js 8+
npm install autoresponse
Usage
The autoresponse
mock config, you can specify by a autoresponse-config.js
config file or passing the mock config params.
var autoresponse = require("autoresponse")({
logLevel: "info",
post: true,
patch: true,
get: {
match: function(reqPathName) {
return !/\.\w+(\?.*)?$/.test(reqPathName);
}
},
rules: [
{
match: "/users/:id",
method: ["get", "patch"]
},
{
match: function(reqPathName, reqMethod) {
return true;
},
mock: function(reqPathName, reqMethod) {
return "custom/myMockFile.js";
},
method: "post"
}
]
});
By default the mock file path is the same as the request url pathname, e.g., the request pathname is /a/b
, the default mock file path is <projectRoot>/mock/a/b.js
. If the mock file is not existed, autoresponse
will auto create the mock file basing the mock file template.
The mock file like this:
module.exports = function(path, queryParam, postParam, context) {
return {
timeout: 50,
_timeout: 50,
_status: 200,
_header: {},
_data: {},
_jsonp: false,
_callback: "callback"
};
};
If the same request need to mock different request method, you can also write the mock data, like this:
module.exports = {
post: function(path, queryParam, postParam, context) {
var params = context.params;
return {
};
},
patch: {
status: 0,
statusInfo: "patch ok"
}
};
Autoresponse
supports any file types mocking, you can using js file
, json file
or any other custom mock syntax to generate the mock data. For example, you can using js
to mock smarty
template without needing php
programming. If there is none available mock handler, you can also custom it by yourself.
Moreover, autoresponse
provide some useful mock helpers to help generating mock data.
Tip: If you need modify the mock config frequently, the best choice is using autoresponse-config.js
config file, autoresponse
support auto reload config file by using watch: true
option without having to restart the dev server. The more usage information you can see here.
The more detail usage, you can see examples.
Using as a connect middleware
var autoresponse = require("autoresponse")({
logLevel: "info",
post: true
});
app.use(autoresponse);
var serveStatic = require("serve-static");
app.use(serveStatic("./webroot"));
Using in webpack-dev-server
webpack-dev-server is developed based on express
, so autoresponse
can as a middleware served it using setup
option.
var compiler = Webpack(webpackConfig);
var server = new WebpackDevServer(compiler, {
before: function(app) {
var autoresponse = require("autoresponse");
app.use(
autoresponse({
logLevel: "debug",
root: projectRootPath,
post: true,
patch: true
})
);
}
});
server.listen(8888, function() {
console.log("Starting server on port 8888...");
});
Using in edp-webserver
If you use EDP solution, you can also use autoresponse
middleware in edp-webserver to mock the http request.
exports.getLocations = function() {
return [
{
location: "/",
handler: home("index.html")
},
{
location: /\.html\b.*$/,
handler: [file()]
},
require("autoresponse")("edp", { watch: true, logLevel: "info" }),
{
location: /^.*$/,
handler: [file(), proxyNoneExists()]
}
];
};
Using mock config file
Create autoresponse
middleware:
var autoresponse = require("autoresponse")({
watch: true
});
Create autoresponse-config.js
file in your web document root, the config file content like the following:
module.exports = {
responseDir: "./mock",
get: [
{
match: "/b.html",
mock: "c.html"
},
{
match: "/account/getUserInfo",
mock: {
proxy: "localhost:9090"
}
},
{
match: "/user/profile"
},
{
match: "/data/list",
mock: "data/list.json"
},
{
match: "/php",
mock: {
path: "/data/test.php"
}
},
{
match: "/a/b",
mock: "a/b.php"
},
"/account/getUserInfo",
function(reqPath, context) {
return {
match: "a/b"
};
}
]
};
Using smarty processor
Convert json
data to html
or html segment
using smarty
template engine.
prepare:
-
install php-cgi
-
processor configure
processor: {
smarty: {
initerFile: './initer.php',
php: {bin: '<php-cgi path>'}
}
}
<?php
error_reporting(0);
ini_set('error_reporting', E_ALL & ~E_NOTICE);
$project_root = dirname(dirname(__FILE__));
require dirname($project_root) . '/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->force_compile = true;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';
$smarty->setTemplateDir($project_root . '/templates/');
$smarty->setCompileDir($project_root . '/templates_c/');
Using mock helper method
By default, if you use js file to mock, you can access mock
global variable in your mock file.
The following methods are provided by default:
-
mock._
: lodash variable
-
mock.m
: dayjs variable,using moment before the 0.3.0
version
-
mock.fake(format, locale)
: the encapsulation of faker
mock.fake("{{name.firstName}}-{{name.lastName}}");
-
mock.fakeCN(format)
: generate chinese locale random information
-
mock.fakeEN(format)
: is equivalent to mock.fake(format)
, generate english locale random information
-
mock.faker(locale)
: get faker
instance with the specified locale, the locale argument is default english
More details, please refer to the autoresponse-config.js.