模块化的简介
首先我们的一般项目需要分为三个模块 Service、Controller、Model
MVC的设计模式。
model层就是实体类,对应数据库的表。
controller层是Servlet,主要是负责业务模块流程的控制,调用service接口的方法,在struts2就是Action。
Service层主要做逻辑判断。
Dao层是数据访问层,与数据库进行对接。
Mapper是mybtis框架的映射用到,mapper映射文件在dao层用。
调用的流程是这样的
前端==》Service==》Controller==》Model==》数据库
第一步:路由的配置
//引入路由koa-router的库
const Router = require('koa-router')
//创建路由的对象
const router = new Router()
const city = require('../appService/cityService')
router.all('/', city.home)
router.all('/add', city.add)
router.all('/updata', city.updated)
//导出路由的模块
module.exports = router;
第二步:Service的配置
//Service
const cityController = require('../appController/cityController')
const cityService = {
home(ctx) {
let res = cityController.home();
ctx.body = {
res: res
}
},
add(ctx) {
let data = {
//接收前端传来的参数
cityName: ctx.params.cityName,
cityId: ctx.params.cityName
}
//把接收的参数传递给cityController
let res = cityController.add(data);
ctx.body = {
res: res
}
},
updated(ctx) {
let res = cityController.updated();
ctx.body = {
res: res
}
},
}
module.exports = cityService;
第三步:cityController的配置
const appModel = require('../appModel/cityModel')
const cityController = {
home() {
let res = appModel.home()
return res
},
add(add) {
let res = appModel.add(add)
return res
},
updated() {
let res = appModel.updated()
return res
},
}
module.exports = cityController;
第四步:appModel 的配置
const appModel = {
home() {
return "Model首页"
},
add(add) {
return {
msg: "Model添加",
add: { //接收传过来的参数
cityName: add.cityName,
cityId: add.cityId,
age: 12
}
}
},
updated() {
return "Model更新"
},
}
module.exports = appModel;
为了我们可以直观和方便的查看我们的接口,我们可以写一个xxx.html的文件,用于存放我们的接口链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>node的第一个程序</title>
</head>
<body>
//接口的链接地址就是我们在路由中配置的路由地址
<p><a href="http://127.0.0.1:5000">城市的首页</a></p>
<p><a href="http://127.0.0.1:5000/add">城市的添加</a></p>
<p><a href="http://127.0.0.1:5000/updata">城市的更新</a></p>
</body>
</html>
到目前为止,我们基本从前端==》Service==》Controller==》Model==》数据库已经打通了,来看一下效果吧
接口的链接
当我们点击其中一个链接都能打开,说明已经成功了。我们以城市的添加为例
这是城市添加的接口
第五步:跨域和中间件请求的配置get和post的配置
为了能够让其他域名的计算机能够访问到,我们需要解决跨域的问题
那么,我们来新建一个文件夹plugins
,在里面我们再新建两个文件请求,分别存放跨域和请求的处理。
文件结构如下:
//cros.js 跨域设置,
module.exports = async(ctx, next) => {
ctx.set("Access-Control-Allow-Origin", "*");
ctx.set("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");
// 请求头设置
ctx.set(
"Access-Control-Allow-Headers",
`Content-Type, Content-Length, Authorization,
Accept, X-Requested-With , yourHeaderFeild,x-token,sessionToken,token`
);
if (ctx.method == "OPTIONS") {
ctx.body = 200;
} else {
await next();
}
}
//params.js
// 处理get和post请求,把参数存到一个新的对象params
module.exports = async(ctx, next) => {
ctx.params = {
...ctx.request.query,
...ctx.request.body
}
await next();
}
这五步配置完后,看我们的app.js文件,最终效果如下:
//引入koa框架库
const Koa = require('koa');
// 创建koa框架库的实例对象
const app = new Koa();
const koaBody = require('koa-body')
app.use(koaBody()); //请放在跨域的代码前
//引入跨域的中间件
const cros = require('./plugins/cros')
app.use(cros)
// 引入请求参数的中间件
const params = require('./plugins/params')
app.use(params)
//导入路由的模块
let router = require('./router/index')
// 使用路由,注意这里router和routes的区别
app.use(router.routes())
app.listen(5000, function() {
console.log('服务已启动,在 http://127.0.0.1:5000')
});
网友评论