代码拉取完成,页面将自动刷新
同步操作将从 this_lucky/ynode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
= 6.0.0
2017-02-22
2017-02-20
2017-02-18
2017-02-17
|- index.js | |- node_modules 目录 | |- public 目录 | |- app 项目目录 | | | |-- apis | | | |-- controllers 普通控制器目录 | | | |-- user 用户组目录 | | | | | |-- IndexController.js 用户组下的控制器 | | |-- OtherController.js | | | |-- goods 商品组目录 | | | | | |-- IndexController.js | | |-- OtherController.js | | | -- views 普通控制器模板目录 | | | |-- user 用户组模板 对应上面用户组 | | | | | |-- index.html | | |-- other.html | | | -- goods 商品组模板 | | | | | |-- index.html | | |-- other.html | | | -- modules 模块 | | | |-- reg | | | | | |-- controllers 模块控制器目录 其下无子目录 | | | | | | | |-- IndexController.js | | | | | |-- views 模块模板目录 | | | | | | | |-- index.html | | | | | |-- 其他目录 | | | -- runtime 缓存目录 |
路由格式
// mvc
http://xxx.com/[route_prefix|moduleId]/[controllerId]
// restful api
http://xxx.com/[pattern]
模块控制器 --> 普通控制器
有用户自定义路由优先解析
// 首先添加别名
Y.setPathAlias('@libs', '/www/libs');
// 这时就可以使用别名创建某一个类的对象了
Y.createObject('libs/Mylib');
npm install ynode
或者
通过 github 下载源码放置到 node_modules 目录即可
项目入口 index.js
var YNode = require('ynode');
new YNode({
'id': 1,
'debug': true,
'appPath': __dirname + '/app',
'assets': 'public',
'modules': {
'bbs': 'app/modules/bbs'
},
'routes': {
// 访问此自定义路径跳转到 bbs 模块 参数为数字 id 及字符串 name
'/abc/{id:\\d+}/{name:\\w+}': {
'moduleId': 'bbs'
}
},
'log': {
'targets': {
'file': {
'class': 'y/log/file/Target'
}
}
}
}).listen(8080, function(){
console.log('listen on 8080');
});
app\controllers\index\IndexController.js
'use strict';
var YNode = require('ynode');
class IndexController extends YNode.WebController {
// 控制器单入口
run(req, res) {
this.getTemplate('index', (err, str) => {
res.end(str);
YNode.Logger.getLogger().error('this is a error log');
YNode.Logger.getLogger().flush();
});
}
}
module.exports = IndexController;
使用 ejs 集成
npm install ejs
在控制器中可以这样做
'use strict';
var YNode = require('ynode');
var ejs = require('ejs');
class IndexController extends YNode.WebController {
run(req, res) {
// 获取 index 模板内容用 ejs 渲染输出
this.getTemplate('index', (err, str) => {
str = ejs.render(str, {user: {name:'张三'}});
res.end(str);
});
// 获取模板全路径使用 ejs 渲染输出
//ejs.renderFile(this.getTemplateFilePath('index'), {user: {name:'张三'}}, function(err, str){
// res.end(str);
//});
}
}
module.exports = IndexController;
// 增加 useRestful 参数启用 RESTful
var app = new YNode({
'id': 1,
'debug': true,
'appPath': __dirname + '/app',
'useRestful': true
});
app.listen(8090, function(){
console.log(8090)
});
// get 路由 并指定 id 参数必须为数字
YNode.WebRestful.get('/abc/{id:\\d+}', function(req, res, id){
var r = new YNode.WebRequest(req);
console.log(r.getQueryString('id'));
console.log(id);
res.end('api get');
});
// 多方法路由 id 参数可为字母或数字
YNode.WebRestful.addRoute(['GET', 'POST'], '/def/{id:}', function(req, res, id){
res.end(id);
});
// 使用 app/api/User 类的 index 方法处理请求
YNode.WebRestful.get('/xyz/{id:}', 'app/apis/User@index');
// 其中 User 的定义如下
'use strict';
class User {
index(req, res, id) {
res.end(id);
}
}
module.exports = User;
可以使用 express 的 body-parser 中间件
YNode.Hook.getInstance().addHook(bodyParser.urlencoded({ extended: false }));
结果会存放到 req.body
关于何时使用 const var 以及 let 一直思考 但一直没定夺
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。