本文整理汇总了TypeScript中koa-session.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript koa-session.default方法的具体用法?TypeScript koa-session.default怎么用?TypeScript koa-session.default使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类koa-session
的用法示例。
在下文中一共展示了koa-session.default方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Koa
import * as Koa from 'koa';
import * as session from 'koa-session';
import {ip, port} from '../config/server';
import renderApp from './render-app';
const app = new Koa();
app.use(session(app));
app.use(renderApp);
app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`[init] listening on ${ip}:${port}`);
});
export default app;
示例2: Koa
decode("123");
hash("abc");
const app = new Koa();
app.use(session({
valid: (ctx, sess) => {
const { session: s } = ctx;
if (s) {
s.sess = "validated";
s.save();
return true;
}
return false;
},
store: {
get: async (key) => {
return "abc";
},
set: (key, val) => {
console.log(key, val);
},
destroy: (key) => {
console.log(key);
},
},
}, app));
app.listen(3000);
示例3: require
import * as session from 'koa-session';
import * as Koa from 'koa';
import RedisStore = require('koa2-session-redis');
const app = new Koa();
const CONFIG = {
store: new RedisStore({
host: '127.0.0.1',
port: 6379,
max_attempts: 0
})
};
app.use(session(CONFIG, app));
示例4: next
// const debug = true
import * as Koa from 'koa'
import * as session from 'koa-session'
import * as logger from 'koa-logger'
import * as serve from 'koa-static'
import * as body from 'koa-body'
import * as cors from 'kcors'
import router from '../routes'
import config from '../config'
const app = new Koa()
let appAny: any = app
appAny.counter = { users: {}, mock: 0 }
app.keys = config.keys
app.use(session(config.session, app))
if (process.env.NODE_ENV === 'development') app.use(logger())
app.use(async(ctx, next) => {
await next()
if (ctx.path === '/favicon.ico') return
ctx.session.views = (ctx.session.views || 0) + 1
let app: any = ctx.app
if (ctx.session.fullname) app.counter.users[ctx.session.fullname] = true
})
app.use(cors({
credentials: true
}))
app.use(async(ctx, next) => {
await next()
if (typeof ctx.body === 'object' && ctx.body.data !== undefined) {
ctx.type = 'json'
示例5: next
};
app.keys = ["some secret hurr"];
// 错误处理
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
errorHandler(ctx, next, err);
}
});
app.use(bodyParse());
app.use(session(sessionConfig, app));
// app.use((ctx, next) => {
// const userName = ctx.session.user || "";
// });
app.use(apiRouters.routes()).use(apiRouters.allowedMethods());
app.use((ctx, next) => {
// 根据状态码抛出相应错误
const statusCode = ctx.response.status;
switch (statusCode) {
case 404:
throw new ResourceNotFoundError(ctx.request.method, ctx.request.url, "没有找到您所需要的资源");
}
});