koa2 快速开始
环境准备
- 因为 node.js v7.6.0 开始完全支持 async/await,不需要加 flag,所以 node.js 环境都要 7.6.0 以上
- node.js 环境 版本 v7.6 以上
- 直接安装 node.js 7.6:node.js 官网地址 https://nodejs.org
- nvm 管理多版本 node.js:可以用 nvm 进行 node 版本进行管理
- Mac 系统安装 nvm https://github.com/creationix/nvm
- windows 系统安装 nvm https://github.com/coreybutler/nvm-windows
- Ubuntu 系统安装 nvm https://github.com/creationix/nvm
- npm 版本 3.x 以上
快速开始
安装 koa2
# 初始化 package.json
npm init
# 安装 koa2
npm install koa
hello world 代码
const Koa = require('koa')
const app = new Koa()
app.use( async ( ctx ) => {
ctx.body = 'hello koa2'
})
app.listen(3000)
console.log('[demo] start-quick is starting at port 3000')
启动 demo
由于 koa2 是基于 async/await 操作中间件,目前 node.js 7.x 的 harmony 模式下才能使用,所以启动的时的脚本如下:
node index.js
访问 http:localhost:3000 ,效果如下
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

上一篇: koa2 概览介绍
下一篇: 彻底找到 Tomcat 启动速度慢的元凶
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论