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. centos 7 源代码搭建部署 zabbix-4.0.13 LTS

    Zabbix 官网 >:https://www.zabbix.com/download 源代码地址>:https://www.zabbix.com/cn/download_sources# ...

  2. 一、CentOS 7安装部署GitLab服务器

    一.CentOS 7安装部署GitLab服务器 1.安装依赖软件 yum -y install policycoreutils policycoreutils-python openssh-serve ...

  3. maven的配置和eclipse maven插件安装

    1.下载maven:http://maven.apache.org/download.cgi 2. 配置环境变量: 3. 修改maven文件夹下bin/conf/settings.xml:maven仓 ...

  4. Date 对象

    JavaScript Date 对象 Date 对象 Date 对象用于处理日期与时间. 创建 Date 对象: new Date() 以下四种方法同样可以创建 Date 对象: var d = ne ...

  5. 一键生成 dao service serverImpl controller 层

    package com.nf147.policy_publishing_platform.util.auto; import java.io.File; import java.io.FileWrit ...

  6. 【Winform-自定义控件】ImageButton 支持鼠标正常、悬停、按下更改图片,支持文本

    原文地址:https://www.codeproject.com/Articles/29010/WinForm-ImageButton 自定义winfrom图片按钮:支持鼠标正常.悬停.按下更改图片, ...

  7. docker使用 Flannel(etcd+flannel)网络

    一.Flannel网络简介 Flannel是一种基于overlay网络的跨主机容器网络解决方案,也就是将TCP数据包封装在另一种网络包里面进行路由转发和通信,Flannel是CoreOS开发,专门用于 ...

  8. MFC CTreeCtrl 递归遍历算法

    递归遍历 void Traverse(HTREEITEM hTree) { if (!hTree) { return; } //Do Something. //Traverse Child Node ...

  9. 转:玩转HTML5移动页面(动效篇)

    作为一名前端,在拿到设计稿时你有两种选择: 1.快速输出静态页面 2.加上高级大气上档次狂拽炫酷屌炸天的动画让页面动起来 作为一个有志向的前端,当然是选2啦!可是需求时间又很短很短,怎么办呢? 这次就 ...

  10. mysql gis基本使用

    # 插入空间数据 INSERT INTO `t_pot` VALUES ('1', '北京', POINT(116.401394,39.916042)); INSERT INTO `t_pot` VA ...