一、如何访问controller创建好的Action
Step1:创建Controller
方法1,利用命令
thinkjs controller user [-r]
// user是需要创建的指定文件名;
// -r 是可选参数,如果添加则创建的是REST Controller,会自动加上rest.js 类
会创建如下文件:
create : src/controller/rest.js // 有加-r参数才会创建此文件
create : src/controller/test.js
create : src/logic/test.js
方法2,手动创建
在src/controller/及src/logic/目录下新建立user.js文件, 这两个目录下对应的文件名必须要一致
Step2:创建Action执行方法
在src/controller/test.js添加如下方法:
module.exports = class extends think.Controller {
indexAction () {
this.body = 'hello test index!';
}
getAction () {
this.body = 'hello test get!';
}
infoAction() {
this.body = 'hello test info!';
}
};
因logic目录是用来添加校验方法,添加的方法实际应该与controller内对应的文件的方法一一对应,这里暂不做校验,即先不添加
Step3:创建router及路由访问
路由解析说明:默认的路由解析规则为 /controller/action,如果是多模块项目,那么规则为 /module/controller/action,根据这个规则解析出对应的 module、controller、action 值。
GET
module.exports = [
['/test'], // 1.
['/test/myGet', 'test/get'], // 2.
['/custom/info', 'test/info'], // 3.
];
如果 controller 有子级,那么会优先匹配子级 controller,然后再匹配 action。
1.默认只引用controller,则http://127.0.0.1:8360/test默认访问到的将会是indexAction;其他的action需要访问刚直接加上对应的action名称,如http://127.0.0.1:8360/test/get访问getAction方法,http://127.0.0.1:8360/test/info访问infoAction方法
2.、3.自定义pathname,指定将getAtcion的访问路径配置为/test/myGet则文件路径为http://127.0.0.1:8360/test/myGet
POST
待续
二、adapter适配器(scr/config/adpter.js)
exports.model = {
type: 'mysql',
common: {
logConnect: isDev,
logSql: isDev,
logger: msg => think.logger.info(msg)
},
mysql: { // 连接本地数据库的配置,以下备注需要根据自己本地的mysql配置决定
handle: mysql,
database: 'demo_database', // 指定数据库名
prefix: 'demo_', // 添加表格名的前缀,查找指定的数据表名可能会为“demo_user”
encoding: 'utf8',
host: '127.0.0.1', // 本地主机
port: '3306', // 端口号
user: 'root', // 本地连接mysql用户名
password: 'mysql2018', // 本地连接mysql用户root的用户密码
dateStrings: true
}
};