代码拉取完成,页面将自动刷新
- 只有 静态资源 [ public ] 目录下的脚本文件 采用 ES7 编码规范
- 其它 脚本文件都采用 CommonJS 编码规范
├── /bin/ # 服务入口
├── /config/ # 应用配置
├── /controllers/ # 控制器
├── /dist/ # 静态资源(生产)[ 由自动化构建工具 Gulp.js 构建 - 请无视它 ]
├── /middlewares/ # 中间件
├── /public/ # 静态资源(开发)[ 图片 脚本 样式 等静态资源都在这里存放或码代 ]
│ ├── /images # 图片(使用 gulp-imagemin 压缩)
│ ├── /javascripts # 脚本(采用 ES7 编码规范, 并使用 gulp-uglify 压缩)
│ └── /stylesheets # 样式(采用 PreCSS 预处理, 并使用 cssnano 压缩)
├── /routes/ # 路由
├── /utils/ # 工具
├── /views/ # 视图
├── .editorconfig # 定义代码格式
├── .eslintrc.js # ES7语法规范配置
├── .gitignore # git忽略文件
├── app.js # 应用入口
├── gulpfile.js # Gulp配置
├── LICENSE # 版权信息
├── package-lock.json # 项目依赖包
├── package.json # 项目依赖包
└── README.md # 项目文档
Step 1, 新建路由 routes/index.js
const compose = require('koa-compose')
const router = require('koa-router')()
const IndexCtrl = require('../controllers/IndexCtrl')
router.get('/', IndexCtrl.index)
module.exports = compose([
router.routes(),
router.allowedMethods()
])
Step 2, 使用路由 routes/routes.js
const compose = require('koa-compose')
module.exports = compose([
require('./index')
])
Step 3, 新建控制器 controllers/IndexCtrl.js
const axios = require('../utils/axios')
const config = require('../config')
module.exports = class IndexCtrl {
// index
static async index(ctx, next) {
// axios(url, data, method)
const res = await axios('/journalismApi')
const data = {
layout: `${config.viewsPath}/layout.html`,
title: 'Hello Koa2 block!',
tech: res.data.tech
}
await ctx.render('index.html', data)
}
}
Step 4, 新建视图 views/index.html
{% extends layout %}
{% block head %}
<link rel="stylesheet" href="/stylesheets/style.css">
{% endblock %}
{% block content %}
<h1>{{ title }}</h1>
<p>Welcome to {{ title }}</p>
<p><img src="/images/IMG_7566.jpg" alt="keyboard"></p>
<h2>科技新闻</h2>
<ul class="tech">
{% for val in tech %}
<li>
<a href="{{ val.link }}" target="_blank">{{ loop.index }}, {{ val.title }}</a>
</li>
{% endfor %}
</ul>
<script type="text/javascript" src="/javascripts/script.js"></script>
{% endblock %}
Step 5, 请移步到 [ public ] 目录继续码代, 图片 脚本 样式 等静态资源都在这处理
├── /public/ # 静态资源(开发)
│ ├── /images # 图片(使用 gulp-imagemin 压缩)
│ ├── /javascripts # 脚本(采用 ES7 编码规范, 并使用 gulp-uglify 压缩)
│ └── /stylesheets # 样式(采用 PreCSS 预处理, 并使用 cssnano 压缩)
Step 1, 安装依赖:
# 安装依赖
yarn
# 或
npm i
Step 2, 开发:
# 启动开发环境 http://localhost:8080/
npm start
# 或
npm run dev
Step 3, 部署:
# 部署生产环境
npm run prd
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型