web框架koa学习笔记

#! /usr/bin

#搭建koa的环境与demo

mkdir koa_demo
cd koa_demo
npm init -y
npm install koa
npm install koa-logger
npm install koa-onerror
npm install koa-static
npm install koa-route
npm install koa-router
npm install koa-ejs
npm install fs.promised
touch app.js;mkdir bin;mkdir routes;mkdir public;mkdir views;
mkdir public/img;mkdir public/css;mkdir public/js;touch public/js/test.js;touch routes/index.js;touch user.js;touch views/_layout.html;touch views/index.html;
echo "var a = '测试';alert('测试');" > public/js/test.js


#准备一个静态html的文件便于测试
echo "



hello world!

" > views/demo.html



#01根据类型返回
echo "const Koa = require('koa');
const app = new Koa();
const main = ctx => {
  if (ctx.request.accepts('xml')) {
    ctx.response.type = 'xml';
    ctx.response.body = 'Hello World';
  } else if (ctx.request.accepts('json')) {
    ctx.response.type = 'json';
    ctx.response.body = { data: 'Hello World' };
  } else if (ctx.request.accepts('html')) {
    ctx.response.type = 'html';
    ctx.response.body = fs.createReadStream('./views/demo.html');
  } else {
    ctx.response.type = 'text';
    ctx.response.body = 'Hello World';
  }
};

app.use(main);
app.listen(8030);" > sample01.js


#02加载一个静态网页
echo "const Koa = require('koa');
const app = new Koa();
const fs = require('fs');
const main = ctx => {
    console.log(Date.now() + ctx.request.method + ctx.request.url);
    ctx.response.type = 'html';
    ctx.response.body = fs.createReadStream('./views/demo.html');
};
app.use(main);
app.listen(8030);" > sample02.js

#03原生路由
echo "const Koa = require('koa');
const app = new Koa();
const fs = require('fs');
const main = ctx => {
  if (ctx.request.path == '/app'){
    ctx.response.type = 'html';
    ctx.response.body = 'app page';
  } else if (ctx.request.path == '/list'){
    ctx.response.type = 'html';
    ctx.response.body = 'list page';
  } else if (ctx.request.path !== '/') {
    ctx.response.type = 'html';
    ctx.response.body = 'Index Page';
  } else {
    ctx.response.body = 'Hello World';
  }
};
app.use(main);
app.listen(8030);" > sample03.js

#04路由模块 koa-route
echo "const Koa = require('koa');
const app = new Koa();
const fs = require('fs');
const route = require('koa-route');

const about = ctx => {
  ctx.response.type = 'html';
  ctx.response.body = 'Index Page';
};
const main = ctx => {
  ctx.response.body = 'Hello World';
};

app.use(route.get('/', main));
app.use(route.get('/about', about));
app.listen(8030);" > sample04.js


#05静态资源目录
#如果出错 koa-static\index.js:39 ,请升级node v7.6.0+
#测试地址:127.0.0.1:8030/js/test.js
echo "const Koa = require('koa');
const app = new Koa();
const path = require('path');
const serve = require('koa-static');
const public_path = serve(path.join(__dirname,'public'));
app.use(public_path);
app.listen(8030);" > sample05.js

#06 重定向
echo "const Koa = require('koa');
const app = new Koa();
const path = require('path');
const route = require('koa-route');
const redirect = ctx => {
  ctx.response.redirect('/');
  ctx.response.body = 'Index Page';
};
app.use(route.get('/redirect', redirect));
app.listen(8030);" > sample06.js

#07 日志中间件
echo "const Koa = require('koa');
const app = new Koa();
const route = require('koa-route');

const logger = (ctx, next) => {
  console.log(Date.now() + ctx.request.method + ctx.request.url);
  next();
}
const redirect = ctx => {
  ctx.response.redirect('/');
  ctx.response.body = 'Index Page';
};
const main = ctx => {
  ctx.response.body = 'Hello World';
};
app.use(logger);
app.use(main);
app.use(route.get('/redirect', redirect));
app.listen(8030);" > sample07.js


#08 中间件顺序

echo "const Koa = require('koa');
const app = new Koa();
const one = (ctx, next) => {
  console.log('>> one');
  //next();
  console.log('<< one');
}
const two = (ctx, next) => {
  console.log('>> two');
  next(); 
  console.log('<< two');
}
const three = (ctx, next) => {
  console.log('>> three');
  next();
  console.log('<< three');
}

app.use(one);
app.use(two);
app.use(three);

app.listen(8030);" > sample08.js


#09异步中间件
echo "
const fs = require('fs.promised');
const Koa = require('koa');
const app = new Koa();

const main = async function (ctx, next) {
  ctx.response.type = 'html';
  ctx.response.body = await fs.readFile('./views/demo.html', 'utf8');
};

app.use(main);
app.listen(8030);" > sample09.js

#10 koa-compose模块可以将多个中间件合成为一个

echo "
const Koa = require('koa');
const app = new Koa();
const compose = require('koa-compose');
const logger = (ctx, next) => {
  console.log(Date.now() + ctx.request.method + ctx.request.url);
  next();
}
const main = ctx => {
  ctx.response.body = 'Hello World';
};
const middlewares = compose([logger, main]);
app.use(middlewares);
app.listen(8030);" > sample10.js


你可能感兴趣的:(web框架koa学习笔记)