在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:cnpm/koa-middlewares开源软件地址:https://github.com/cnpm/koa-middlewares开源编程语言:JavaScript 100.0%开源软件介绍:koa-middlewareseasy way to use some small but useful koa middlewares. PRs are welcome, but only for those small and general middlewares. installnpm install koa-middlewares --save middlewares
see exports Usagevar koa = require('koa');
var middlewares = require('koa-middlewares');
var router = middlewares.router();
var app = koa();
router.get('/', function *(){
this.body = 'hello koa-middlewares';
});
app.use(middlewares.bodyParser());
app.use(middlewares.conditional());
app.use(middlewares.etag());
app.use(middlewares.compress());
middlewares.csrf(app);
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(7001); Middlewares Quick Guide
app.use(middlewares.bodyParser({
limit: '10mb'
}));
app.use(function *(next) {
var postBody = this.request.body;
});
middlewares.csrf(app);
app.use(function *checkCsrf(next) {
if (this.method === 'GET' ||
this.method === 'HEAD' ||
this.method === 'OPTIONS') {
return yield *next;
}
this.assertCsrf();
yield next;
});
middlewares.render(app, {
root: path.join(__dirname, 'view')
});
app.use(function *() {
yield this.render('page.html', {foo: 'bar'});
});
app.use(middlewares.conditional());
app.use(middlewares.etag());
app.use(middlewares.favicon());
middlewares.jsonp(app);
app.use(function* () {
this.jsonp = {foo: 'bar'};
});
app.use(middlewares.logger());
app.use(middlewares.cookieSession());
app.use(middlewares.session({
store: middlewares.RedisStore(),
defer: true
}));
app.use(function *() {
var session = yield this.session;
session.foo = 'bar';
this.body = this.session.foo;
});
var router = middlewares.router();
router.get('/', function *() {
this.body = 'Hello koa-router';
});
app.use(router.routes());
app.use(router.allowedMethods());
var users = new middlewares.Resource('users');
app.use(users.middleware());
app.get('/users', function *() {
this.body = [{name: 'Lee'}, {name: 'Han'}];
});
app.use(middlewares.rewrite('/js/*', '/public/assets/js/$1'));
var microtime = require('microtime');
app.use(middlewares.rt({
timer: microtime
}));
app.use(middlewares.staticCache(path.join(__dirname, 'public'), {
buffer: true,
maxAge: 60 * 60 * 24 * 7,
dir: path.join(rootdir, 'public')
}));
var app = koa()
app.use(compress({
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}))
var app = koa()
onerror(app); License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论