下載模塊
npm install koa
或者咆耿,配置文件package.json中配置依賴如下
"dependencies": {
"koa": "^2.13.1"
...
}
hello world
代碼
// 導(dǎo)入koa聚霜,和koa 1.x不同薛耻,在koa2中捌斧,我們導(dǎo)入的是一個class,因此用大寫的Koa表示:
const Koa = require('koa');
// 創(chuàng)建一個Koa對象表示web app本身:
const app = new Koa();
// 對于任何請求鬼悠,app將調(diào)用該異步函數(shù)處理請求:
app.use(async (ctx, next) => {
await next();
ctx.response.type = 'text/html';
ctx.response.body = '<h1>Hello, koa2!</h1>';
});
// 在端口3000監(jiān)聽:
app.listen(3000);
console.log('app started at port 3000...');