#! /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 "<html>
<head>
</head>
<body>
hello world!
</body>
</html>" > 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 = '<data>Hello World</data>';
} 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 = '<a href="/">Index Page</a>';
} 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 = '<a href="/">Index Page</a>';
};
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 = '<a href="/">Index Page</a>';
};
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 = '<a href="/">Index Page</a>';
};
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学习笔记
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- python web开发学习心得,原创文章,转载请注明出处 1.创建应用程序 2.定义模型,创建一个名为Topic...
- python web开发学习心得,原创文章,转载请注明出处 1.建立一个learning_log文件夹,在其中搭建...
- 关注一航文学与艺术每一天都与众不同 踏雪而行 文/天涯觅梦 是时候下一场雪了 一场纷纷扬扬的大雪 让这城市与...