advanced
Advanced tools
Comparing version 0.3.7 to 0.3.8
@@ -37,3 +37,3 @@ var Express = require('express'), | ||
if (Utils.isDev() && Utils.c('isMock')) { | ||
app.use(require('./middlewares/mock')); | ||
app.use(require('./middlewares/mock')()); | ||
} | ||
@@ -40,0 +40,0 @@ |
@@ -9,36 +9,43 @@ /** | ||
module.exports = function(req, res, next) { | ||
// 设置所有api地址为本地 | ||
var localApi = req.protocol + '://' + req.get('host'); | ||
Utils.c('apis', _.mapValues(Utils.c('apis'), function() { | ||
return localApi; | ||
})); | ||
module.exports = function(pathFilter) { | ||
return function(req, res, next) { | ||
// 设置所有api地址为本地 | ||
var localApi = req.protocol + '://' + req.get('host'); | ||
Utils.c('apis', _.mapValues(Utils.c('apis'), function() { | ||
return localApi; | ||
})); | ||
if (req.get('X-Forwarded-By') === 'Advanced') { | ||
var reqPath = req.path.substring(1); | ||
reqPath = reqPath.replace(/\/+/g, '/').replace(/\/$/, ''); | ||
if (req.get('X-Forwarded-By') === 'Advanced') { | ||
var reqPath = req.path.substring(1); | ||
reqPath = reqPath.replace(/\/+/g, '/').replace(/\/$/, ''); | ||
var path = Path.join(Utils.c('mockDataPath'), (reqPath || 'index') + '.json'); | ||
if (fs.existsSync(path)) { | ||
res.json((fs.readJsonSync || fs.readJsonFileSync)(path)); | ||
} else { | ||
// 将最后一个单元当做参数,再试一次 | ||
var arr = reqPath.split('/'); | ||
if (arr.length >= 2) { | ||
arr.pop(); | ||
var path; | ||
if (_.isFunction(pathFilter)) { | ||
path = pathFilter(req, reqPath); | ||
} else { | ||
path = Path.join(Utils.c('mockDataPath'), (reqPath || 'index') + '.json'); | ||
} | ||
if (fs.existsSync(path)) { | ||
res.json((fs.readJsonSync || fs.readJsonFileSync)(path)); | ||
} else { | ||
// 将最后一个单元当做参数,再试一次 | ||
var arr = reqPath.split('/'); | ||
if (arr.length >= 2) { | ||
arr.pop(); | ||
var path1 = arr.join('/'); | ||
path1 = Path.join(Utils.c('mockDataPath'), path1 + '.json'); | ||
if (fs.existsSync(path1)) { | ||
res.json((fs.readJsonSync || fs.readJsonFileSync)(path1)); | ||
var path1 = arr.join('/'); | ||
path1 = Path.join(Utils.c('mockDataPath'), path1 + '.json'); | ||
if (fs.existsSync(path1)) { | ||
res.json((fs.readJsonSync || fs.readJsonFileSync)(path1)); | ||
} else { | ||
next(new Error('Mocking api ' + req.path + ', but does not exist data file ' + path + ' or ' + path1)); | ||
} | ||
} else { | ||
next(new Error('Mocking api ' + req.path + ', but does not exist data file ' + path + ' or ' + path1)); | ||
next(new Error('Mocking api ' + req.path + ', but does not exist data file ' + path)); | ||
} | ||
} else { | ||
next(new Error('Mocking api ' + req.path + ', but does not exist data file ' + path)); | ||
} | ||
} else { | ||
next(); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "advanced", | ||
"version": "0.3.7", | ||
"version": "0.3.8", | ||
"description": "A simple MVC framework based on Express", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
36621
889