4.0.1 • Published 5 years ago
eefserver v4.0.1
使用说明
创建项目
- npm run new
<projectName>
启动
- npm start
<projectName> - 预览模式:
- npm start
<projectName>-- -p - npm start
<projectName>-- --preview - 自动配置.featurerc
- npm start
<projectName>-- -a - npm start
<projectName>-- --auto
部署
- npm run release
<projectName> - npm release
<projectName> - npm release
<projectName>-- -p - npm release
<projectName>-- --preview
开发配置文件 更目录下.featurerc
- 示例文件
{ "configName": "huitong", "disableLive": false, "proxy": "https://dev.ehking.com", "zhe ending": "line." } configName: string本地开发使用哪个配置文件(configs目录下的配置)disableLive?: boolean停用liveload自动刷新proxy: string要代理的接口域名
项目配置文件说明
- 示例:
module.exports = { hosts: [ "dev-lark.ehking.com", "qa-lark.ehking.com", "lark.ehking.com", ], forwards: { "lark.ehking.com" : (api) => { return api.substring(7) } }, prefix: 'v2', uri: (domain) => { if (domain === "member.allscore.com") { return '' } return '/member' }, locals: [ siteTitle: '洋墨水', ], proxies: [ '/cas', '/logout', '/lark/cas', '/api/', ], routes: { index: { url: '/', method: 'get', controller: IndexController, }, login: { url: '/login', method: 'post' controller: require('../server/overseas/LoginController') }, }, } hosts: string | string[]支持的域名locals?: null | { [key: string]: any }默认模板常量proxies?: string[]代理到后端服务器的接口 本地开发环境时使用routes: Route路由地址配置规则type Route = { [matchUrlString: string]: { url: string; method?: string; controller: (req, res) => void; }, }forwards?: Forward地址替换规则,用于测试环境与生产环境接口路径不一致type Forward = { [domain: string]: (api: string) => string }prefix?: string | (domain: string) => string所有地址前缀,项目上线时避免冲突uri?: string | (domain: string) => string后端页面网址规则
服务端控制器编写
res.request: (options: IRequestOptions) => IRequestResponse返回对象结构enum METHODS = { GET: 'GET' POST: 'POST' get: 'get' post: 'post' } interface IRequestOptions { method: METHODS; url: string; qs?: { [key: string]: any }; formData: { [key: string]: any } } interface IApiResponse { // 接口返回的 `hasError` 字段 hasError: boolean; // 接口返回 `message` 字段 message: string; // 接口返回 `data` 字段 data: any; } interface IRequestResponse extends IApiResponse { // http请求返回的 `原始response对象` raw: { [key: string]: any; }; // http请求响应的 `正文文本` body: string; }
Swig模板全局变量
routes: string路由地址表 如 routes.index -> '/'currentRoute: string当前路由地址 如:/login,/sitePrefix: string网址前缀locals: { [key: string ]: any }配置文件里的locals常量
Swig自定模板过滤器
uri构造网址 如:<a href="{{ routes.search | uri({id: 1}) }}">详情</a>number_format格式化数值, 参数(fixedLength=2, sep=' ', sepLen=3)interface INumberFormat (fixedLength: string, sep?: string, sepLen: number) => string<p>{{ 2000 | number_format(2,',', 3) }}</p> <-- 2,000.00 -->strlen: (length: numbeer, adder: string) => string字符串长度截取, 参数(length, adder)<p>{{ content | strlen(3, '...') }}</p> <-- 内容是... -->
服务端req、res对象修改
req.request请求后端接口的方法res.view渲染模板 封装res.renderinterface IView (path: string, data: { [key: string] : any }) => voidres.tell发送json请求 封装res.jsoninterface ITell ({ [key: string] : any }) => void
Fis模板注意事项
- 所有静态资源使相对路径
./,../ - js中引用资源地址
__uri('./a.js')
参考文档
- Express 4 服务器支撑
- Swig 模板引擎
- Request 服务端http请求组件
- Bootstrap jQuery UI组件
- Checkit 字段验证组件
- Layer Ui
- Coffee Script
- jQuery
- Fis 百度前端解决方案
todos
windows 下release 问题测试环境路径与生产环境路径不同- 静态资源上传支持缓存
- 项目上线如果网址子路径不同,测试不能测出的问题
FAQ
4.0.1
5 years ago