A simple MVC framework based on Express
npm install advancedA simple MVC framework based on Express
```
npm install advanced -g
`shell`create demo
advanced new appinstall app
cd app
npm installstart app
node server.js
Visit http://localhost:8586
Controllers are placed in app/controllers.
For example app/controllers/test.js:
`javascript
var Controller = require('advanced').Controller;
module.exports = Controller.extend({
index: function() {
this.res.send('Hello World!');
}
});
`
Visit http://localhost:8586/test
Router inherits Router of Express. But there are some enhanced features.
You can use a string to specify a controller and a method. i.e. controller@method
In app/routes.js
`javascript
var Router = require('advanced').Router,
router = Router();
// a string to specify a controller and a method. i.e. controller@method
router.get('/test/add/:id(\\d+)', 'test@addTest');
module.exports = router;
`
Specify routes with group. It likes router.use. The difference is that you don't need to create a router manually.
`javascript
var Router = require('advanced').Router,
router = Router();
router.group('/group', function(router) {
router.get('/a', 'test@a'); // /group/a
router.get('/b', 'test@b'); // /group/b
}
module.exports = router;
`
A simple router based on file of controller is supported.
If doesn't match any custom router, it will go here.
For example:
`javascript`
req.path = '/this/is/a/path'
1. is exists /this/is/a/path/index.js@index/this/is/a/path.js@index
2. is exists /this/is/a.js@path
3. is exists /this/is.js@a
4. is exists , this.req.params[0] = 'path'
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.
For example. Assume that the request path is /test/api. If there is a json file whose 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 a template, in development mode, it will output json data that will be rendered to the template.
For example: Visit http://127.0.0.1:8586/?debug=true will get
`json`
{
"test": 1
}
This method uses request module.
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.defaults') which is assigned to this._api
`javascript`
{
dataKey1: '/path1',
datakey2: '/path2'
}
The data returned likes below.
`javascript`
{
dataKey1: {...},
dataKey2: {...}
}
The response data will be assigned to the corresponding key totally. But you can filter the response data by overriding the _filerData method.
`javascript
module.exports = Controller.extend({
_filterData: function(data) {
return data.data;
},
index: function() {
this.request({
dataKey1: '/path1',
datakey2: '/path2'
}).then(function(data) {
this.render('index.swig', data);
}.bind(this));
}
})
`
You can pass data to destination when you create a request. Any options that can be passed to request modulerequest
also can be passed to method.
1. qs object containing querystring values to be appended to the uriform
2. object to be passed to destination like to submit a form.
For example:
`javascript`
module.exports = Controller.extend({
index: function() {
this.request({
dataKey1: {
uri: '/path1',
qs: {
name: 'Javey'
},
form: {
password: '123'
}
},
datakey2: '/path2'
}).then(function(data) {
this.render('index.swig', data);
}.bind(this));
}
})
You don't need to do anything, When you want to forward a request(req). The apiProxy middleware can do anything for you.host
It can forward a request which is created by AJAX to the server transparently.
Config file is placed in config/. If the filename starts with config, it will be loaded automatically.
* @param key {String|Object}value
@param {}
@return {} The config you get or set.
Use Utils.c(key, value) to set config. The key can be a object, if you want set a block of config.
If the value depends the other one. You can specify it like below:
`js`
var conf = {
a: 'a',
ab: '{a}b' // ab depends a
}
Utils.c(conf);
Use Utils.c(key) to get config. The key is a string.
`js``
Utils.c('ab'); // the value is 'ab'
MIT