koa--教程1

koa2 快速开始

环境准备

  • 由于node.js v7.6.0开始彻底支持async/await,不须要加flag,因此node.js环境都要7.6.0以上
  • node.js环境 版本v7.6以上
  • 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

node index.js复制代码

访问http:localhost:3000,效果以下git

start-result-01


原文链接:https://github.com/ChenShenhai/koa2-note/blob/master/note/start/quick.mdgithub

相关文章
相关标签/搜索