# 工作目录
lixin-macbook:Desktop lixin$ pwd
/Users/lixin/Desktop
# 创建项目工程
lixin-macbook:Desktop lixin$ mkdir koa-example
# 初始化骨架
lixin-macbook:koa-example lixin$ npm init -y
# 下载koa
lixin-macbook:koa-example lixin$ cnpm install koa --save
// 引入koa
const Koa = require("koa");
const app = new Koa();
// 配置中间件(类似于Java中的Filter)
app.use(async (ctx) => {
ctx.body = "hello world koa2";
});
// 监听端口
app.listen(8080);
lixin-macbook:koa-example lixin$ tree -L 1
.
├── app.js
├── node_modules
├── package-lock.json
└── package.json
lixin-macbook:koa-example lixin$ node app.js
lixin-macbook:~ lixin$ curl http://localhost:8080
hello world koa2