1.项目目录

2.路由

根目录/routes/index.js -- 首页

const router = require('koa-router')();

router.get('/', async (ctx) => {
await ctx.render('default/index');
}) // 注意 前台和后台匹配路由的写法不一样
router.get('/case', async (ctx) => {
ctx.body = '案例'
}) router.get('/about', async (ctx) => {
await ctx.render('default/about');
}) module.exports = router.routes();

根目录/routes/api.js -- api接口

const router = require('koa-router')();

router.get('/', async (ctx) => {
ctx.body = {
"title": "这是一个api"
}
}) router.get('/newslist', async (ctx) => {
ctx.body = {
"title": "这是一个新闻接口"
}
}) router.get('/focus', async (ctx) => {
ctx.body = {
"title": "这是一个轮播图的api"
}
}) module.exports = router.routes();

根目录/routes/admin.js -- 后端管理

const router = require('koa-router')();
const user = require('./admin/user.js');
const focus = require('./admin/focus.js');
const newscate = require('./admin/newscate.js'); // 配置admin的子路由(层级路由)
router.get('/', async (ctx) => {
ctx.body = '后台管理系统首页';
}) router.use('/user', user)
router.use('/focus', focus)
router.use('/newscate', newscate) module.exports = router.routes();

根目录/routes/admin/user.js -- 用户

/**
* 用户的增加修改删除
*/
const router = require('koa-router')(); router.get('/', async (ctx) => {
// ctx.body = '用户首页';
await ctx.render('admin/user/index');
}) router.get('/add', async (ctx) => {
await ctx.render('admin/user/add');
}) router.get('/edit', async (ctx) => {
await ctx.render('admin/user/edit');
}) router.get('/delete', async (ctx) => {
ctx.body = '删除用户';
}) module.exports = router.routes();

根目录/routes/admin/focus.js -- 轮播图

/**
* 轮播图的增加修改删除
*/
const router = require('koa-router')(); router.get('/', async (ctx) => {
await ctx.render('admin/focus/index');
}) router.get('/add', async (ctx) => {
await ctx.render('admin/focus/add');
}) router.get('/edit', async (ctx) => {
await ctx.render('admin/focus/edit');
}) router.get('/delete', async (ctx) => {
ctx.body = '删除轮播图';
}) module.exports = router.routes();

根目录/routes/admin/newscate.js -- 新闻分类

/**
* 新闻分类的增加修改删除
*/
const router = require('koa-router')(); router.get('/', async (ctx) => {
ctx.body = '新闻分类首页';
}) router.get('/add', async (ctx) => {
ctx.body = '增加新闻分类';
}) router.get('/edit', async (ctx) => {
ctx.body = '编辑新闻分类';
}) router.get('/delete', async (ctx) => {
ctx.body = '删除新闻分类';
}) module.exports = router.routes();

3.视图

根目录/views/default/index.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>Document</title>
</head> <body>
<h2>这是一个网站的首页</h2>
<div>
box
</div>
</body> </html>

4.入口文件

/**
* koa 路由模块化
*/
const Koa = require('koa');
const router = require('koa-router')();
const path = require('path');
const render = require('koa-art-template');
// 引入子模块(子路由)
const admin = require('./routes/admin.js');
const api = require('./routes/api.js');
const index = require('./routes/index.js'); // 实例化
var app = new Koa(); // 配置 koa-art-template 模板引擎
render(app, {
root: path.join(__dirname, 'views'),
extname: '.html',
debug: process.env.NODE_ENV !== 'production'
}) // 配置路由
// router.use('/', index)
router.use(index) /**
* /admin 配置子路由(层级路由)
* /admin/user
*/
router.use('/admin', admin); /**
* /api/newslist 新闻列表的api
*/
router.use('/api', api); /*在模块里面暴露路由并且启动路由*/ // 启动路由
app.use(router.routes()).use(router.allowedMethods()) app.listen(8008);

.

koa 路由、视图模块化(二)的更多相关文章

  1. Laravel教程 二:路由,视图,控制器工作流程

    Laravel教程 二:路由,视图,控制器工作流程 此文章为原创文章,未经同意,禁止转载. View Controller 上一篇教程我们走了那么长的路,终于把Laravel安装好了,这一篇教程我们就 ...

  2. koa 路由模块化(一)

    1.项目目录 2.入口文件 根目录/app.js /** * koa 路由模块化 */ const Koa = require('koa'); const router = require('koa- ...

  3. koa 路由配置

    Koa 路由 路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET.POST 等) 组成的,涉及到应用如何响应客户端对某个网站节点的访问. 通俗的讲:路由就是根据不 ...

  4. Vue-初步了解vue-router的三要素:路由map 、路由视图、路由导航

    安装vue-router模块 使用vue-router前要先安装vue-router库 cnpm install vue-router –save 使用vue-router vue-router有三个 ...

  5. Xamarin XAML语言教程模板视图TemplatedView(二)

    Xamarin XAML语言教程模板视图TemplatedView(二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于中TemplatedView.代码如下: <? ...

  6. 用Decorator控制Koa路由

    在Spring中Controller长这样 @Controller public class HelloController{ @RequestMapping("/hello") ...

  7. Eclipse的Debug各种视图介绍(二)

    本文链接:https://blog.csdn.net/u011781521/article/details/55000066    http://blog.csdn.net/u010075335/ar ...

  8. vue 路由视图,router-view嵌套跳转

    实现功能:制作一个登录页面,跳转到首页,首页包含菜单栏.顶部导航栏.主体,标准的后台网页格式.菜单栏点击不同菜单控制主体展示不同的组件(不同的页面). 配置router-view嵌套跳转需要准备两个主 ...

  9. Laravel 5系列教程二:路由,视图,控制器工作流程

    免费视频教程地址https://laravist.com/series/laravel-5-basic 上一篇教程我们走了那么长的路,终于把Laravel安装好了,这一篇教程我们就要进入Laravel ...

随机推荐

  1. centos7 开放/关闭防火墙和端口

    --------------------------------------------------------------防火墙----------------------------------- ...

  2. 数据集:Introduction to Econometrics by Stock&Watson

    James H. Stock and Mark W. Watson, Introduction to Econometrics: data sets 詹姆斯·H·斯托克 马克·W·沃森. 计量经济学. ...

  3. nginx反向代理局域网访问外网

    .配置内网hosts vim /etc/hosts 添加 host1(能连外网的服务器ip)   central.maven.org 2.在host1 服务器上nginx配置 server {    ...

  4. 中断或取消Promise链的可行方案

    ES6标准引入的异步编程解决方案Promise,能够将层层嵌套的回调转化成扁平的Promise链式调用,优雅地解决了“回调地狱”的问题.当Promise链中抛出一个错误时,错误信息沿着链路向后传递,直 ...

  5. Win10 OpenCV3.3.0+VS2013配置大坑,OpenCV解决方案编译报错“找不到python36_d.lib”错误

    今天因为想要用OpenCV做图像识别,小白一个,在网上找到一个教程,但是需要配置OpenCV3.3.0的环境,于是又在网上找OpenCV3.3.0+VS2013(因为我之前已经安过了VS2013),前 ...

  6. shell脚本中使用nohup执行命令不生效

    1 例如 !#/bin/bash nohup echo "hello world" 2 解决办法 加上 source /etc/profile 就好了 !#/bin/bash so ...

  7. Lambda方法推导(method references)

    在上一篇[http://www.cnblogs.com/webor2006/p/7707281.html]中提到了方法推导的东东: 这里说细的学习一下它,下面走起! Method references ...

  8. 安装及创建python虚拟环境

    有点气,是真的有点气,以为安装错误了,没想到是命令问题 参考链接: https://cloud.tencent.com/developer/article/1176291 https://www.cn ...

  9. pip安装第三方库时提示No Module named pip

    因为多次卸载/安装python不同版本导致出现pip模块不存在的问题,原因可能是环境没有配置好执行下面命令 ----完成

  10. ubuntu16下安装MySQLdb

    2016年07月20日 10:37:04 tonydandelion2014 阅读数 3354更多 分类专栏: Python   1.使用pip安装 pip install mysql-python ...