Koa2 MVC

安装依赖

$ npm init ./treasure
$ cd treasure
$ npm i -S koa
$ npm i -S koa-router
$ npm i -S koa-static-cache
$ npm i -S koa-bodyparser
$ npm i -S koa-session
$ npm i -S art-template
$ npm i -S koa-art-template
$ npm i -S path

依赖组件

$ cat package.json
{
  "name": "treasure",
  "version": "0.0.1",
  "private": false,
  "dependencies": {
    "art-template": "^4.13.2",
    "koa": "^2.11.0",
    "koa-art-template": "^1.1.1",
    "koa-bodyparser": "^4.2.1",
    "koa-router": "^7.4.0",
    "koa-session": "^5.12.3",
    "koa-static-cache": "^5.1.2",
    "path": "^0.12.7"
  }
}

项目结构

文件描述
config配置文件夹,配置文件使用JSON格式,配置内容根据运行环境分为development和production。
public静态资源文件存放目录
view视图模板文件存放目录
controller控制器文件存放目录
app.js应用启动文件
route.js路由配置文件

应用入口

$ vim app.js
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const staticCache = require("koa-static-cache");
const session = require("koa-session");
const render = require("koa-art-template");
const {join} = require("path");

//创建应用
const app = new Koa();

//获取环境变量
const env = app.env || "development";
const debug = env === "development";

//路径设置
const basePath = join(__dirname, "public");
const configPath = join(__dirname, "config");
const viewPath = join(__dirname, "view");
const controllerPath = join(__dirname, "controller");

//应用配置
const appConfig = require(join(configPath, "app"))[env];

//会话配置
const sessionConfig = require(join(configPath, "session"))[env];
app.use(session(sessionConfig, app));

//使用静态文件解析器
app.use(staticCache(basePath));

//使用body解析器
app.use(bodyParser());

//使用模板引擎中间件
render(app, {root:viewPath, extname:".html", debug:debug});

//应用上下文设置属性
app.context.controllerPath = controllerPath;

//路由配置
require("./route")(app);

//监听端口 开启服务
app.listen(appConfig.port, _=>{
  console.log("web server is starting");
});

配置文件

应用配置

$ vim config/app.json
{
  "development": {
    "port": 3001
  },
  "production": {
    "port": 3001
  }
}

会话配置

$ config/session.json
{
  "development": {
    "key": "session_key",
    "maxAge": 604800000,
    "overwrite": true,
    "httpOnly": true,
    "signed": true,
    "rolling": true,
    "renew": false
  },
  "production": {
    "key": "session_key",
    "maxAge": 604800000,
    "overwrite": true,
    "httpOnly": true,
    "signed": true,
    "rolling": true,
    "renew": false
  }
}

路由配置

$ route.js
const router = require("koa-router")();
const path = require("path");

module.exports = app=>{
    //获取控制器路径
    const controllerPath = app.context.controllerPath;

    //加载控制器
    const IndexController = require(path.join(controllerPath, "index"));

    //路由规则
    router.get("/", IndexController.index);

    //应用使用路由中间件
    app.use(router.routes()).use(router.allowedMethods());
};

控制器

$ controller/index.js
exports.index = async (ctx, next)=>{
    console.log(ctx.controllerPath);
    const title = "homepage";
    const content = "hello world";
    await ctx.render("index", {title, content});
};

视图

$ vim view/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
</head>
<body>
{{content}}
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值