NodeJ Koa2 安装使用 reeber
介绍
Koa 是由 Express 原班人马打造的,致力于成为一个更小、更富有表现力、更健壮的 Web 框架。 使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的回调函数嵌套, 并极大地提升错误处理的效率。
koa 不在内核方法中绑定任何中间件, 它仅仅提供了一个轻量优雅的函数库,使得编写 Web 应用变得得心应手。(来自kao官网)
基于async/await实现中间体系的koa2框架将会是是node.js web开发方向大势所趋的普及框架。基于generator/yield的koa1将会逐渐被koa2替代,毕竟使用co.js来处理generator是一种过渡的方式,虽然有其特定的应用场景,但是用async/await会更加优雅地实现同步写法。
安装
- koa 依赖node V7.6.0及以上版本,
- 首先确认node版本在7.6.0以上,版本低的请自行搞定。
npm init // 初始化package.json
npm i koa // 安装koa2
常用的引入模板
- 我们一般是不会这样直接返回的,实际开发中一般都是返回一个模版文件,我们可以先让koa读取这个文件,然后返回到用户界面。
首先先创建一个views的目录,在里面新建一个template.html文件内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello koa2</title>
</head>
<body>
<h1>Hello Koa2!</h1> </body>
</html>Html前台页面
将app.js中的内容修改为如下:
const fs = require('fs');
const Koa = require('koa');
const app = new Koa(); app.use(async (ctx) => {
// ctx.body = 'hello koa2'
ctx.type = 'html';
ctx.body = fs.createReadStream('./views/template.html');
}); app.listen(1029);
console.log('koa damo is starting at port 1029');JS Koa代码
基础结构语法
const Koa = require('koa');
const Router = require('koa-router'); let server = new Koa();
let router = new Router(); server.listen(8080); router.get('/a', async ctx=>{
ctx.body='aaaaa';
}); server.use(router.routes())const Koa = require('koa');
const Router = require('koa-router'); let server = new Koa();
let router = new Router(); server.listen(8080); router.get('/a', async ctx=>{
ctx.body='aaaaa';
}); server.use(router.routes())Koa基础结
有哪些 属性?
- ctx.method 请求方法
- ctx.url
- ctx.path
- ctx.query ?user=Haisen&id=1
- ctx.params 数据参数
- ctx.ip 客户端IP
多层路由的分层写法
- 一级路由
一层选择 主路由 server.use(router.routes()); - 二层路由
- 一级路由
二层路由 1.引入路由对象 2.创建路由 3.配置路由 4.对外使用
- const Koa = require('koa');
const Router = require('koa-router');let server = new Koa();
server.listen(8080);
let router = new Router();router.get('/',async ctx=>{
ctx.body = '根目录'
});router.use('/home',require('./routes/home'));
// router.use('/admin',require('./routes/admin'));server.use(router.routes())
Koa2一层路由
const Router = require('koa-router');
// 嵌套二级
let home = new Router();
home.get('/',ctx=>{
ctx.body ='前端';
});
home.get('/login', async ctx=>{
ctx.body='login';
});
home.get('/sigin', async ctx=>{
ctx.body='sigin';
});
module.exports=home.routes();
Koa2二层路由
NodeJ Koa2 安装使用 reeber的更多相关文章
- 安装 NodeJ Koa2、3 + 独立插件 cli脚手架 npm cnpm Vue
安装 NodeJ npm cnpm Koa2.3 + 独立插件 cli脚手架 Vue 安装 在 这里写过了 这两个分开了写 Nodej:下载 node.js 安装 10.0版 ...
- koa2入门--01.ES6简单复习、koa2安装以及例子
1.ES6简单复习 /*let 和 const: let用于定义一个块作用域的变量,const 定义一个常量 */ let a = 'test'; const b = 2; /*对象的属性和方法的简写 ...
- koa2安装
安装 1. npm install koa-generator -g 2. Koa2 test-koa2 3. npm install & npm run dev 看package.json里 ...
- koa2第一天 安装koa2
安装全局koa2:npm install -g koa2 -generator 创建一个koa2文件夹:koa2 -e koa2 进入koa2文件夹:cd koa2 安装npm模块:npm insta ...
- koa2第一天 安装koa2found 1 low severity vulnerability run `npm audit fix` to fix them, or `npm audit` for details
安装全局koa2:npm install -g koa2 -generator 创建一个koa2文件夹:koa2 -e koa2 进入koa2文件夹:cd koa2 安装npm模块:npm insta ...
- koa2入门使用总结
koa2的介绍 Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小.更富有表现力.更健壮的基石. 通过利用 async ...
- 从入门到不放弃系列之Koa2
一.Koa2入门 本来是想Express入门的,但是既然都是要学,干嘛不学最新的呢? 其实我想说,我本来只是想学个小程序开发,现在已经陆陆续续开了好多坑了.. 本文参考廖雪峰教程 二.Async 最新 ...
- Koa2 的安装运行记录(二)
参考 :koa2-boilerplate https://github.com/superalsrk/koa2-boilerplate Ajax Login and Ajax Logout in ...
- Koa2 的安装运行记录(一)
1.参考koa+react(一) http://blog.suzper.com/2016/10/19/koa-react-%E4%B8%80/ 为了使用 KOA2 能够运行,必须能够使用ES7语法 a ...
随机推荐
- 基础架构之日志管理平台及钉钉&邮件告警通知
接上一篇,我们继续解释如何把ELK跟钉钉及发送邮件功能结合起来,让我们及时的了解重要日志并快速反馈. Sentinel 安装,项目介绍在https://github.com/sirensolution ...
- How to use Log4cplus
Introduction Log4cplus is derived by the popular Log4j written in java.<br>This tutorial show ...
- js常用代码记录
1.动态key得到某对象中相对应的value js中想根据动态key得到某对象中相对应的value的方法有二: var key = "name1";var value = obj[ ...
- C++ Notes 1 - size_type - Accelerated Ch3
1. 为什么用string::size_type而不是int? --Why use string::size_type ? int is supposed to work! it holds numb ...
- GridControl 应用 z
DevExpress学习系列(控件篇):GridControl的基本应用 一般属性设置 不显示分组框:Gridview->Option View->Show Group Panel=fal ...
- Python学习---重点模块之shelve
简单示例 import shelve f = shelve.open(r'shelve.txt') f['info'] = {'name':'ftl', 'age':23, 'sex': 'male' ...
- January 21 2017 Week 3 Saturday
Courage is grace under pressure. 勇气就是压力下的优雅. In the face of stress, can you deal with your task smoo ...
- 如何将程序添加到Windows桌面右键菜单
在Windows桌面上右键单击鼠标时,将显示默认菜单.如果您想要将其它程序添加到Windows桌面右键菜单中,则可以按照以下步骤执行: 运行WinUtilities上下文菜单管理器 点击添加 选择菜单 ...
- 如何删除Word 2010中的“向下箭头”
原文:https://jingyan.baidu.com/article/e75aca85552916142edac614.html 在日常办公中,如果从网站复制了一段文字,直接粘贴到Word中时,常 ...
- Hibernate多对一关联关系
两个持久化类.Customer 和 OrderForm Customer 类. package com.zcd.hibernate.manyToOne; public class Customer { ...