Comparing version 0.1.8 to 0.1.9
@@ -6,2 +6,3 @@ module.exports = { | ||
routesPath: '{root}/app/routes.js', | ||
logPath: '{root}/log', | ||
@@ -8,0 +9,0 @@ defaultController: 'index', |
@@ -5,4 +5,5 @@ var Controller = require('advanced').Controller; | ||
index: function() { | ||
//this.res.redirect('/404') | ||
this.render('index.swig', {test: 1}); | ||
} | ||
}); |
@@ -9,3 +9,3 @@ /** | ||
router.get('/test/add/:id(\\d+)', 'test@addTest'); | ||
router.group('/group', 'test@auth', function(router) { | ||
router.group('/group', /*'test@auth',*/ function(router) { | ||
router.get('/a', 'test@groupA'); | ||
@@ -12,0 +12,0 @@ router.get('/b', 'test@groupB'); |
@@ -20,3 +20,3 @@ var Path = require('path'); | ||
//isMock: true | ||
isMock: true | ||
}; |
@@ -8,14 +8,14 @@ var Advanced = require('advanced'), | ||
var app = Advanced(); | ||
var app = Advanced(function(app) { | ||
app.engine('swig', swig.renderFile); | ||
app.set('views', Path.join(__dirname, 'views')); | ||
app.set('view engine', 'swig'); | ||
app.set('view cache', false); | ||
swig.setDefaults({cache: false}); | ||
app.engine('swig', swig.renderFile); | ||
app.set('views', Path.join(__dirname, 'views')); | ||
app.set('view engine', 'swig'); | ||
app.set('view cache', false); | ||
swig.setDefaults({cache: false}); | ||
app.use('/static', Advanced.Express.static(Path.join(__dirname, 'static'))); | ||
}); | ||
app.use('/static', Advanced.Express.static(Path.join(__dirname, 'static'))); | ||
app.listen(Advanced.Utils.c('port'), function() { | ||
console.log('App is listening on the port ' + Advanced.Utils.c('port')); | ||
}); |
@@ -22,2 +22,5 @@ var Express = require('express'), | ||
// access log | ||
app.use(require('./middlewares/accessLog')); | ||
// 传入一个函数,自定义app的行为 | ||
@@ -30,8 +33,13 @@ _.isFunction(fn) && fn(app); | ||
Utils.c('api', 'http://127.0.0.1:' + Utils.c('port')); | ||
app.use(require('./mock')); | ||
app.use(require('./middlewares/mock')); | ||
} | ||
var router = loadRoutes(); | ||
router.simpleRoute(); | ||
app.use('/', router); | ||
// custom router | ||
app.use(loadRoutes()); | ||
// simple router | ||
app.use(require('./middlewares/simpleRouter')); | ||
// 404 | ||
app.use(require('./middlewares/404')); | ||
// 500 | ||
app.use(require('./middlewares/500')); | ||
@@ -38,0 +46,0 @@ return app; |
@@ -50,61 +50,3 @@ var Express = require('express'), | ||
return this.use.apply(this, middlewares); | ||
}, | ||
/** | ||
* 实现对文件的简单路由,如果找不到路由配置,则执行该规则 | ||
* @example | ||
* req.path = '/this/is/a/path' | ||
* 1. 是否存在/this/is/a/path/index.js@index | ||
* 2. 是否存在/this/is/a/path.js@index | ||
* 3. 是否存在/this/is/a.js@path | ||
* 4. 是否存在/this/is.js@a,传入参数path | ||
*/ | ||
simpleRoute: function() { | ||
this.use(function(req, res, next) { | ||
var reqPath = req.path.substring(1); | ||
reqPath = reqPath.replace(/\/+/g, '/').replace(/\/$/, ''); | ||
var arr; | ||
if (!reqPath) { | ||
arr = []; | ||
} else { | ||
arr = reqPath.split('/'); | ||
} | ||
arr.push(Utils.c('defaultController')); | ||
var action = 'index', | ||
param = undefined, | ||
path = arr.join('/'); | ||
var iter = 0, | ||
count = Math.min(4, arr.length); | ||
while (iter < count && !runController(path, action, param, req, res, next)) { | ||
iter++; | ||
path = arr.slice(0, -iter).join('/'); | ||
var _arr = arr.slice(-iter); | ||
action = _arr[0]; | ||
if (_arr.length > 2) { | ||
param = _arr[1]; | ||
} | ||
} | ||
if (iter === count) { | ||
next(); | ||
} | ||
}); | ||
} | ||
}); | ||
function runController(path, action, param, req, res, next) { | ||
path = Utils.c('controllerPath') + '/' + path; | ||
if (fs.existsSync(path + '.js')) { | ||
var Ctrl = require(path), | ||
ctrl = new Ctrl(req, res, next); | ||
if (typeof ctrl[action] === 'function') { | ||
if (param != undefined) { | ||
req.params[0] = param; | ||
} | ||
ctrl[action](); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}); |
var Express = require('express'), | ||
methods = require('methods'), | ||
debug = require('debug')('advance:router:route'), | ||
debug = require('debug')('advanced:router:route'), | ||
Layer = require('express/lib/router/layer'), | ||
@@ -5,0 +5,0 @@ utils = require('express/lib/utils'), |
@@ -65,12 +65,34 @@ var _ = require('lodash'), | ||
getFnByString: function(path) { | ||
var controllerPath = Utils.c('controllerPath'), | ||
defaultAction = Utils.c('defaultAction'); | ||
var defaultAction = Utils.c('defaultAction'); | ||
return function(req, res, next) { | ||
var obj = analyzeController(path), | ||
Controller = require(controllerPath + '/' + obj.path), | ||
ctrl = new Controller(req, res, next); | ||
var obj = analyzeController(path); | ||
return Utils.runController(obj.path, obj.action || defaultAction, null, req, res, next); | ||
} | ||
}, | ||
return ctrl[obj.action || defaultAction](); | ||
/** | ||
* 执行一个controller的相应action | ||
* @param path | ||
* @param action | ||
* @param param | ||
* @param req | ||
* @param res | ||
* @param next | ||
* @returns {boolean} | ||
*/ | ||
runController: function(path, action, param, req, res, next) { | ||
path = Utils.c('controllerPath') + '/' + path; | ||
if (fs.existsSync(path + '.js')) { | ||
var Ctrl = require(path), | ||
ctrl = new Ctrl(req, res, next); | ||
if (typeof ctrl[action] === 'function') { | ||
if (param != undefined) { | ||
req.params[0] = param; | ||
} | ||
ctrl[action](); | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
@@ -77,0 +99,0 @@ |
{ | ||
"name": "advanced", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A simple MVC framework based on Express", | ||
@@ -22,4 +22,5 @@ "main": "./lib/index.js", | ||
"methods": "^1.1.1", | ||
"morgan": "^1.5.3", | ||
"request": "^2.55.0" | ||
} | ||
} |
@@ -96,3 +96,3 @@ # Advanced | ||
If set `env = 'development` in `config.js`, it will load the middleware of `mock`. The request which created by invoking `Cotroller::request` method will be mocked. | ||
If set `env = 'development'` and `isMock = true` in `config.js`, it will load the middleware of `mock`. The request which created by invoking `Cotroller::request` method will be mocked. | ||
@@ -103,3 +103,3 @@ For example. Assume that the request path is `/test/api`. If there is a json file which path is `/mock/test/api.json`, the json data will be sent by reading the file. | ||
Add `debug=true` to query string. When you want render template, in development mode, it will output json data which will render the template. | ||
Add `debug=true` to query string. When you want render a template, in development mode, it will output json data that will be rendered to the template. | ||
@@ -117,5 +117,5 @@ For example: Visit [http://127.0.0.1:8586/?debug=true](http://127.0.0.1:8586/?debug=true) will get | ||
Call `Controller::request` to create a request in node. A promise will be returned. The arguments pass to the function likes below. | ||
Call `Controller::request` to create a request in node. A promise will be returned. The arguments pass to the function like below. | ||
> The baseUrl is `Utils.c('api')` which assigned to `this._api` | ||
> The baseUrl is `Utils.c('api')` which is assigned to `this._api` | ||
@@ -122,0 +122,0 @@ ```javascript |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
21880
29
563
6
11
+ Addedmorgan@^1.5.3
+ Addedbasic-auth@2.0.1(transitive)
+ Addedmorgan@1.10.0(transitive)
+ Addedon-finished@2.3.0(transitive)
+ Addedon-headers@1.0.2(transitive)
+ Addedsafe-buffer@5.1.2(transitive)