标签:信息 file 页面 stream views 定向 direct 判断 对话
// demos/01.js const Koa = require(‘koa‘); const app = new Koa(); app.listen(3000);
// demos/02.js const Koa = require(‘koa‘); const app = new Koa(); const main = ctx => { ctx.response.body = ‘Hello World‘; }; app.use(main); app.listen(3000);
// demos/03.js 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 = ‘<p>Hello World</p>‘; } else { ctx.response.type = ‘text‘; ctx.response.body = ‘Hello World‘; } };
// demos/04.js const fs = require(‘fs‘); const main = ctx => { ctx.response.type = ‘html‘; ctx.response.body = fs.createReadStream(‘./demos/template.html‘); };
// demos/05.js const main = ctx => { if (ctx.request.path !== ‘/‘) { ctx.response.type = ‘html‘; ctx.response.body = ‘<a href="/">Index Page</a>‘; } else { ctx.response.body = ‘Hello World‘; } };
// demos/06.js 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));
// demos/12.js const path = require(‘path‘); const serve = require(‘koa-static‘); const main = serve(path.join(__dirname)); app.use(main);
// demos/13.js const redirect = ctx => { ctx.response.redirect(‘/‘); ctx.response.body = ‘<a href="/">Index Page</a>‘; }; app.use(route.get(‘/redirect‘, redirect));
// demos/07.js const main = ctx => { console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`); ctx.response.body = ‘Hello World‘; };
// demos/08.js const logger = (ctx, next) => { console.log(`${Date.now()} ${ctx.request.method} ${ctx.request.url}`); next(); } app.use(logger);
// demos/09.js 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);
// demos/10.js 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(‘./demos/template.html‘, ‘utf8‘); }; app.use(main); app.listen(3000);
// demos/11.js 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);
// demos/14.js const main = ctx => { ctx.throw(500); };
// demos/15.js const main = ctx => { ctx.response.status = 404; ctx.response.body = ‘Page Not Found‘; };
// demos/16.js const handler = async (ctx, next) => { try { await next(); } catch (err) { ctx.response.status = err.statusCode || err.status || 500; ctx.response.body = { message: err.message }; } }; const main = ctx => { ctx.throw(500); }; app.use(handler); app.use(main);
// demos/17.js const main = ctx => { ctx.throw(500); }; app.on(‘error‘, (err, ctx) => console.error(‘server error‘, err); );
// demos/18.js` const handler = async (ctx, next) => { try { await next(); } catch (err) { ctx.response.status = err.statusCode || err.status || 500; ctx.response.type = ‘html‘; ctx.response.body = ‘<p>Something wrong, please contact administrator.</p>‘; ctx.app.emit(‘error‘, err, ctx); } }; const main = ctx => { ctx.throw(500); }; app.on(‘error‘, function(err) { console.log(‘logging error ‘, err.message); console.log(err); });
// demos/19.js const main = function(ctx) { const n = Number(ctx.cookies.get(‘view‘) || 0) + 1; ctx.cookies.set(‘view‘, n); ctx.response.body = n + ‘ views‘; }
// demos/20.js const koaBody = require(‘koa-body‘); const main = async function(ctx) { const body = ctx.request.body; if (!body.name) ctx.throw(400, ‘.name required‘); ctx.body = { name: body.name }; }; app.use(koaBody());
// demos/21.js const os = require(‘os‘); const path = require(‘path‘); const koaBody = require(‘koa-body‘); const main = async function(ctx) { const tmpdir = os.tmpdir(); const filePaths = []; const files = ctx.request.body.files || {}; for (let key in files) { const file = files[key]; const filePath = path.join(tmpdir, file.name); const reader = fs.createReadStream(file.path); const writer = fs.createWriteStream(filePath); reader.pipe(writer); filePaths.push(filePath); } ctx.body = filePaths; }; app.use(koaBody({ multipart: true }));
标签:信息 file 页面 stream views 定向 direct 判断 对话
原文地址:https://www.cnblogs.com/absoluteli/p/14124436.html