xpress
What is xpress?
- Api Framework A node.js api framework base on express which provide api route version and channel control
- Web Framework A node.js web framework base on express and art-templete, and expand many usefull view helper
Install
$ npm install -g xpress
Sample code
there are two full sample projects in /sample/api and /sample/web
Run sample project
#cd ./sample/api
cd ./sample/web
npm install
node app.js
open http://127.0.0.1:8001/, there are many template engine(base on artTemplate) view helper sample code
Create project with commander
npm install -g xpress
cd /<project path>
xpress -p ./ -t (api | web)
npm install
npm start
APi
create an api server with xpress
//import modulevar Xpress = ;var config = ;
//create servervar server = host: null key: null cert: null trace: true //open route trace port: http: 8001 https: null ;
//configureserver;server;server;server;server;server;
Xpress.engine is equal require('art-template/node/template-native.js')
//use middlewarevar body = ;var cookie = ;var timeout = ;var compression = ;var statics = ;server;server;server;server;server;server;
//register an api on a controller with version or channel control//'v' represent version and 'c' represent channel//if you register an api with version and channel control, //you must set 'X-Accept-Version' and 'X-Client-Channel' on request header, //xpress will get the two value from the header and compared with the register v and c//if not equal, xpress will skip the controller and jump to the next controller which has registered the same route//you can get and set the two headers on Xpress.defaults.versionHeader and Xpress.defaults.channelHeaderserver;//register an api on a controller without version and channel controlserver
//create a sub router and register on servervar Router = XpressRouter;var productRouter = ;//register an api on subRouter without version or channel control //import utils var string = Xpressstring;var parser = Xpressparser;var validate = Xpressvalidate;productRouter//register an api on subRouter with version or channel control productRouterserver;
//error handlerserver;server;
//listen on host and portserver;//listen on host and port with cluster//server.cluster(0, function(msg){// console.log(msg);//});
//exportmoduleexports = server;
Integration tools
parser
var parser = parser;parser;parser;parser;parser; //122parser; //100parser; //100parser; //trueparser; //falseparser; //{name:"synder"}parser; //null
fs
var fs = fs;//get dirfs; //return user home dirfs; //return system temp dir//get file name or dirfsfilenamepath; //return file name fs; //return dir name//node navtive fs methodfs;fs;fs;fs;fslink;fs;fs;fs;fs;fs;//fs extend methodfs;fs;fs; //list file or dir in pathfs; //list all file in path and child pathfs;fs; //read file by linefs;fs;fs;fs; //copy file or dirfs; //cur file or dirfs;fs;fs; //get file from web
validate
var validate = validate;validatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidatevalidate
string
var string = string;string //2016-04-20string //10:03:20stringdateTime '-' ':' //2016-04-20 10:03:20string //synder name isstring //'0000012222'string //'1222200000'string //'0001222200'string //10pxstring //180*****442string //10 + 20string //namestring //"name"string //my name isstring //['my', 'name', 'is']string //+10string //-10stringstring //1222123132...stringstringstringstring //$242,605,401.001string //玖佰亿零贰佰陆拾萬零肆佰零壹string //242,605,401.001string //2335 4645 4633 3443 32string //50%string //50.00%string //0.500
art-template)
View helper(base on<%= $dateTime(new Date())%> | 2016年06月07日 11时56分38秒 |
<%= $dateTime(Date.now())%> | 2016年06月07日 11时56分38秒 |
<%= $Math.random()%> | 0.035149546630335315 |
<%= $toString([1,2,3])%> | [1,2,3] |
<%= $parseInt('10', 0)%> | 10 |
<%= $parseInt('NA0122', 0)%> | 0 |
<%= $parseInt('NA0122')%> | null |
<%= $parseFloat('12.3')%> | 12.3 |
<%= $parseFloat('a12.3', 0)%> | 0 |
<%= $format('%s %X', 'sam', 1000)%> | sam 0x3E8 |
<%= $format('%s %o', 'sam', 1000)%> | sam 01750 |
<%= $format('%s %d', 'sam', 1000)%> | sam 1000 |
<%= $format('%s %b', 'sam', 1000)%> | sam 1111101000 |
<%= $format('%c %c', 'sam', 98)%> | sam b |
<%= $format('%s %2f', 'sam', 98)%> | sam 98.00 |
<%= $format('%s %j', 'sam', {name:1})%> | sam {"name":1} |
<%= $toString(null)%> | |
<%= $toString({name: 1})%> | {"name":1} |
<%= $toString([12,3])%> | [12,3] |
<%= $join('10', '2', '6', '+')%> | 10+2+6 |
<%= $join('10', null, '6', '9', '+')%> | 10+6+9 |
<%= $join(null, null, '6', '+')%> | 6 |
<%= $join(null, '6', '', '+')%> | 6 |
<%= $trim(' name ')%> | name |
<%= $mask('18083489462', '*', 4, 5)%> | 180*****462 |
<%= $mask('18083489462', '*', -1, 5)%> | 18083****** |
<%= $pad('12222', 10, '0', 'left')%> | 0000012222 |
<%= $pad('12222', 10, '0', 'right')%> | 1222200000 |
<%= $clean(' 122 22 ')%> | 122 22 |
<%= $toString($lines('122\r\n22132'))%> | [122,22132] |
<%= $toString($lines('122\r22132'))%> | [122,22132] |
<%= $truncate('122212313213132132', 13, '...')%> | 1222123132... |
<%= $chineseCurrency('92102600401.001')%> | 玖佰贰拾壹亿零贰佰陆拾萬零肆佰零壹 |
<%= $currency(242605401.001, '$', 2)%> | $242,605,401.00 |
<%= $upperCase('AbddessSww')%> | ABDDESSSWW |
<%= $lowerCase('AbddessSww')%> | abddesssww |
<%= $capitalize('AbddessSww')%> | AbddessSww |
<%= $capitalize('AbddessSww', true)%> | Abddesssww |
<%= $bankCard('233546454633344332')%> | 2335 4645 4633 3443 32 |
<%= $number(0.5, 3)%> | 0.500 |
<%= $thousands(2783619263)%> | 2,783,619,263 |
<%= $percentage(0.5)%> | 50% |
<%= $percentage(0.523366, 2)%> | 52.34% |
<%= $urlPathVersion('/name', 10)%> | /name?version=10 |
<%= $urlPathJoin('/name', '//age')%> | /name/age |
<%= $urlFormat('/home', {name:1}, 'http', '127.0.0.1')%> | http://127.0.0.1/home?name=1 |
<%= $urlFormat('/home', {name:1})%> | /home?name=1 |
<%= $encodeURIComponent('/测试 账号')%> | %2F%E6%B5%8B%E8%AF%95%20%E8%B4%A6%E5%8F%B7 |
<%= $decodeURIComponent($encodeURIComponent('/测试 账号'))%> | /测试 账号 |
<%= $encodeURI('/测试 账号')%> | /%E6%B5%8B%E8%AF%95%20%E8%B4%A6%E5%8F%B7 |
<%= $decodeURI($encodeURI('/测试 账号'))%> | /测试 账号 |
<%= $date('2016-06-01T07:05:36.838Z', '-')%> | 2016-06-01 |
<%= $time('2016-06-01T07:05:36.838Z', ':')%> | 15:05:37 |
<%= $dateTime('2016-06-01T07:05:36.838Z', '-', ':')%> | 2016-06-01 15:05:37 |
<%= $dateTime('2016-06-01T07:05:36.838Z')%> | 2016年06月01日 15时05分37秒 |
<%= $isNull(null)%> | true |
<%= $isNull(undefined)%> | false |
<%= $isUndefined(undefined)%> | true |
<%= $isUndefined(null)%> | false |
<%= $isNullOrUndefined(null)%> | true |
<%= $isNullOrUndefined(undefined)%> | true |
<%= $isArray([])%> | true |
<%= $isDate(new Date())%> | true |
<%= $isDate('2012-03-01')%> | false |
<%= $isString('name')%> | true |
<%= $isString({})%> | false |
<%= $isString(null)%> | false |
<%= $isNumber(1)%> | true |
<%= $isBool(true)%> | true |
<%= $isInt(1.1)%> | false |
<%= $isInt(1)%> | true |
<%= $isFloat(1.1)%> | true |
<%= $isFloat(1)%> | false |
<%= $isObject(null)%> | false |
<%= $isObject({})%> | true |
<%= $isObject([])%> | true |
<%= $isDictionary([])%> | false |
<%= $isDictionary({})%> | true |
art-template
refer to art-template
express
refer to express.js