# xj_module_caller_backend
**Repository Path**: IBAS0742/xj_module_caller_backend
## Basic Information
- **Project Name**: xj_module_caller_backend
- **Description**: 新疆,系统子模块调用后台模板
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2019-07-03
- **Last Updated**: 2021-09-08
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# xj_module_caller_backend
#### 一、Description
新疆,系统子模块调用后台模板
#### 二、[_watch 和 _watch_server 说明](./_watch.md)
### 三、最最最重要的部分 *工作内容*
> 1.修改 /utils/checkFail.js
```
/**
* @param task 和定义在 module/task.js 一致
* 主要是检查其 log 是否正常
* 这个 log 就是完整日志
* 所以可以考虑在代码结束后输出类似
* [this code is total complete and success]
* 的日志输出,这样只需要检查完整日志中是否有该内容即可
* @return 异常返回 -1 ,正常返回 100
* */
module.exports = (task) => {
if (task.log.indexOf('error') + 1) {
return -1;
} else {
return 100;
}
};
```
> 2.添加或修改 /utils/call/Cmd 内容 [*方法看这里*](./ReadIBAS.md)
> 3.记得添加 /utils/mysqlConf.json 文件(因为不知道能不能提交)
```json
{
"host": "ip",
"user": "user",
"password": "password",
"database": "database"
}
```
### 四、启动方法
```
// 直接启动
node main.js
// 有重启机制的启动
// 1.通过监听文件的方法启动
./_watch/runAll.bat
// 2.通过服务的方法启动
./_watch_server/runAll.bat
```
### 五、模块定义说明 **划重点**
```
因为不知道如何将 babel 配置起来,这里无法使用 es6 语法的
import 和 export
只能使用 amd 模式的
requrie 和 module.exports
```
### 六、package 说明
```
"async": "^2.6.2", // mysql 需要用到
"body-parser": "^1.17.1", // 解码从前端发送过来的json
"connect-multiparty": "^2.2.0",//解码 formdate 对象
"cookie-parser": "^1.4.4", // 解码关于 cookie 方面的
"cors": "^2.8.5", // 允许跨域访问
"express": "^4.13.3", // 整体的服务框架
"mysql": "^2.16.0", // mysql 库
"request": "^2.88.0" // 请求相关(在发布遥感影像和_watch_server中被使用到)
```
### 七、请求返回说明
```javascript
// 返回页面,访问方法 ip:port/error.html
router.get('/error.html',function(req,res) {
// 这里说明返回的内容是一个 html
res.setHeader('Content-Type', 'text/html');
// 这里的 error 对应于 views 中的 error.html
res.render('error');
});
// 返回 json 内容
router.get('/api/getJson',function(req,res) {
res.json({
name: "IBAS"
});
});
```
### 八、前端向后端发送请求
```javascript
// 使用 formdata 方式的 post 请求
var fd = new FormData();
fd.append("name","ibas");
fetch("/api",{
method: "post",
body: fd
});
// 使用 get 请求
fetch("/api?name=ibas",{
method: "get",
})
// 普通的 post 请求
fetch("/api",{
method: "post",
headers: {
'Content-Type': "application/json;charset=UTF-8",
},
body: JSON.stringify({
name: "IBAS"
})
})
```
### 九、utils 文件夹说明
```
mysql.js 是一个数据库类,使用方法如下
mysql = new Mysql({
host: '',
user: '',
password: '',
database: ''
});
更多方法可以参考 test/testMysql.js
建议定义 mysqlConf.json 文件统一配置
-----------------------------------------
R.js 是一个处理 请求 和 响应 对象的类
使用方法为
let R = new RClass({
req,res,requestType
}).analizeReq();
R.content.params // 请求时带的参数可以在这里获取到
R.setError("错误信息")
R.setError({message: "错误信息"})
R.setError(new Error("错误信息"))
R.resCallBack(data,status);
// 相当于调用 res.json({ data,status })
-----------------------------------------
callCmd
这个是配合 ./api/tasks.js 文件定义的,请看 ReadIBAS.md 说明
-----------------------------------------
checkFail.js
用于检查一个任务是否成功
```
### 十、module
```
将以为每一个表建立一个 module 文件,可以参考 draught_source.js 的内容
```
### 十一、调用数据库及 cmd 等定义在 api 中,查看[文档说明](ReadIBAS.md)