Skip to content

Commit

Permalink
param optional and json file auto load and no cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane committed Nov 27, 2014
1 parent c0873db commit 2f737c7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 52 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -20,9 +20,9 @@ $ imock -j json -b api

## Options

-j --json 必填,mock 文件目录,该目录下存放所有处理请求的 js
-j --json 可选,mock 文件目录,该目录下存放所有处理请求的 js

-b --base 必填,指定 mock 请求的 base path
-b --base 可选,指定 mock 请求的 base path(若 base = api,则请求 mock 数据的地址为 http://localhost:{port}/api/{js-filename})

-p --port 可选,指定 mock server 端口号,默认 3000

Expand All @@ -34,6 +34,7 @@ $ imock -j json -b api
1. www 与 base 可以是同层级或 www 是 base 的父目录,base 不能是 www 的父目录;
2. 通常 base 为 /mock 或 /api;
3. 前端 js 中请求地址为 http://localhost:3000/mock 或 http://localhost:3000/api
4. -j, -b 参数须同时使用 或 都不使用


## mock 请求处理文件 example
Expand Down
97 changes: 61 additions & 36 deletions app.js
Expand Up @@ -5,61 +5,86 @@ var url = require('url');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var bytes = require('bytes');
var getport = require('getport');

var rootPath = process.cwd();

module.exports = function (port, dir, www, base) {
var app = express();
module.exports = function(port, dir, www, base) {
var app = express();

var static = path.join(process.cwd(), www);
var static = path.join(process.cwd(), www);

app.set('views', static);
app.engine('html', require('ejs').renderFile);
app.use(morgan('dev'));
app.set('views', static);
app.engine('html', require('ejs').renderFile);
app.use(morgan('dev'));
app.disable('etag');

app.use(function (req, res, next) {
res.set('Cache-Control', 'no-cache');

next();
app.use(function(req, res, next) {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': 0
});

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false,
limit: bytes('10mb')
}));
next();
});

// parse application/json
app.use(bodyParser.json({
limit: bytes('10mb')
}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false,
limit: bytes('10mb')
}));

app.use(app.router);

www = url.resolve('/', www);
// parse application/json
app.use(bodyParser.json({
limit: bytes('10mb')
}));

app.use(www, express.directory(static));
app.use(www, express.static(static));
app.use(app.router);

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
www = url.resolve('/', www);

app.use(www, express.directory(static));
app.use(www, express.static(static));

app.use('/', function(req, res, next) {
res.redirect(www);
});

// development only
if ('development' === app.get('env')) {
app.use(express.errorHandler());
}

if (dir != null) {
rootPath = path.join(rootPath, dir);

if (base[base.length - 1] != '/') {
base += '/';
if (base == null) {
return console.log('缺少参数: -b --base mock 请求 base path');
}

if (base[base.length - 1] !== '/') {
base += '/';
}

base = url.resolve('/', base);
app.all(url.resolve(base, '*'), function (req, res) {
require('./lib/handler').on(req, res, rootPath, base);

app.all(url.resolve(base, '*'), function(req, res) {
require('./lib/handler').on(req, res, rootPath, base);
});
}

getport(port, function(e, p) {
if (e) {
throw e;
}

http.createServer(app).listen(p, function() {
console.log('静态服务器已启动: http://localhost:' + p);

http.createServer(app).listen(port, function() {
console.log('mock 服务器已在(' + port + ')端口启动!');
console.log('通过下面地址访问mock数据:\n http://localhost:' + port + base);
if (dir != null && base != null) {
console.log('mock 服务器已启动: http://localhost:' + p + base);
}
});
});
};
8 changes: 0 additions & 8 deletions bin/imock
Expand Up @@ -28,12 +28,4 @@ program

program.parse(process.argv);

if (!program.json) {
return console.log('缺少参数: -j --json json 文件和配置目录');
}

if (!program.base) {
return console.log('缺少参数: -b --base mock 请求 base path');
}

mocker(program.port || defaultPort, program.json, program.www || defaultWww, program.base);
11 changes: 7 additions & 4 deletions lib/handler.js
Expand Up @@ -13,8 +13,6 @@ function combine(arr, num) {
}

var getFilenames = function (reqPath, mock) {
console.log(reqPath);

var r = [];
var realPath = reqPath.replace(/\/+$/, '').substring(mock.length);

Expand Down Expand Up @@ -71,12 +69,17 @@ var findExists = function (files, rootPath) {

var on = function (req, res, rootPath, base) {
var files = getFilenames(req.path, base.substring(0, base.length - 1));

var exisitsFilename = findExists(files, rootPath);

var mod;
var mod, modPath;

if (exisitsFilename) {
mod = require(path.join(rootPath, exisitsFilename));
modPath = path.join(rootPath, exisitsFilename);

require.cache[require.resolve(modPath)] = null;

mod = require(modPath);

mod[req.method.toLowerCase()](req, res);
} else {
Expand Down
3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "imock",
"version": "3.0.5",
"version": "3.1.0",
"description": "restful mock server for front-end developer",
"main": "app.js",
"bin": {
Expand Down Expand Up @@ -32,6 +32,7 @@
"commander": "^2.3.0",
"ejs": "^1.0.0",
"express": "^3.16.8",
"getport": "^0.1.0",
"morgan": "^1.3.0",
"underscore": "^1.7.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/c.js
Expand Up @@ -4,7 +4,7 @@ module.exports = {
code: 200,
msg: {
shopName: '小南国',
shopId: '1234',
shopId: '124',
level: 5
}
});
Expand Down

0 comments on commit 2f737c7

Please sign in to comment.