describe('flash', function () { var app = koa(); var session = require('koa-session'); var flash = require('koa-flash'); app.keys = ['foo']; app.use(session()); app.use(flash({ key: 'bar' })); render(app, { root: path.join(__dirname, '../example') }); app.use(function *() { this.flash.notice = 'Success!'; yield this.render('flash'); }); it('should success', function (done) { request(app.listen()) .get('/') .expect(/Success/) .expect(200, done); }); });
// debug('renderConf: ', renderConf); merge(renderConf.locals || {}, core, false); app.keys = [renderConf.locals.$app.name]; /** * 中间件使用 */ // 捕捉下游错误 app.use(errorhandler()); app.use(mount('/public', staticServer(__dirname + '/public'))); app.use(bodyparser()); app.use(logger()); app.use(session({ store: new MongoStore(config.mongodb) })); app.use(flash()); app.use(scheme(config.schemeConf)); app.use(render(app, renderConf)); app.use(router(app, { root: config.routerConf })); /** * module.parent 判断文件是否被引用,被引用则导出 app.callback() 已供测试 */ if(module.parent) { module.exports = app.callback(); } else {
import unauthrouter from './unauthrouter'; import router from './routes'; import authMiddleware from './middlewares/authMiddleware'; import cors from 'koa-cors'; const app = new Koa(); app.keys = ['secret']; app.use(logger()); app.use(bodyParser()); app.use(convert(cors())); app.use(convert(session({ store: new MongoStore({ url: "mongodb://127.0.0.1:27017/koav2" }) }))); app.use(convert(flash())); import './auth'; import passport from 'koa-passport'; app.use(passport.initialize()) app.use(passport.session()) render(app, { root: './public', layout: false, viewExt: 'html', cache: false }); app.use(convert(serve(path.join(__dirname, 'assets')))); app.context.render = co.wrap(app.context.render); unauthrouter(app); app.use(authMiddleware());
Server.prototype.start = function () { let logger = Logger({ dir: config.logDir, categories: ['router', 'model', 'controller', 'job'], format: 'YYYY-MM-DD-[{category}][.log]' }); let port = this.opts.port || 3000; this.use(converter(session({ store: redisStore({}) }))); this.keys = ['heavenduke']; this.use(converter(astepback())); this.use(converter(flash({}))); this.context.logger = logger; onerror(this); new koaPug({ viewPath: './view', debug: false, pretty: true, compileDebug: false, app: this }); this.use(converter(bodyParser({ formLimit: "100mb", jsonLimit: "100mb", textLimit: "100mb" }))); this.use(converter(override())); this.use(converter(validator())); //静态文件cache let staticDir = config.staticDir; this.use(converter(koaStatic(path.join(staticDir, 'uploads')))); this.use(converter(staticCache(staticDir))); this.use(converter(staticCache(path.join(staticDir, 'uploads')))); this.use(converter(staticCache(path.join(staticDir, 'dist')))); this.use(converter(staticCache(path.join(staticDir, 'img')))); this.use(converter(staticCache(path.join(staticDir, 'fonts')))); this.use(async (ctx, next) => { try{ await next(); } catch(e) { if (process.env.NODE_ENV != 'test') { console.log(e.message); console.log(e); logger.error(e); } ctx.render('./error'); } }); routerUtils.mount(this, routers); this.listen(port); console.log('listening on port %s',config.port); };
Server.prototype.start = function () { var logger = Logger({ dir: config.logDir, categories: ['router', 'model', 'controller', 'job'], format: 'YYYY-MM-DD-[{category}][.log]' }); var port = this.opts.port || 3000; this.use(session(this)); this.keys = ['heavenduke']; this.use(flash()); this.context.logger = logger; onerror(this); var jade = new koaJade({ viewPath: './view', debug: false, pretty: true, compileDebug: false, app: this }); this.use(bodyParser()); this.use(override()); this.use(validator()); //静态文件cache var staticDir = config.staticDir; this.use(koaStatic(path.join(staticDir, 'uploads'))); this.use(staticCache(staticDir)); this.use(staticCache(path.join(staticDir, 'vendors'))); this.use(staticCache(path.join(staticDir, 'uploads'))); this.use(staticCache(path.join(staticDir, 'js'))); this.use(staticCache(path.join(staticDir, 'img'))); this.use(staticCache(path.join(staticDir, 'css'))); this.use(staticCache(path.join(staticDir, 'fonts'))); this.use(staticCache(path.join(staticDir, 'bower_components'))); plugins.static_init(this); this.use(function *(next) { try{ yield* next; } catch(e) { if (process.env.NODE_ENV != 'test') { console.log(e.message); console.log(e); logger.error(e); } this.render('./error'); } }); this.use(router(this)); appRouter(this); plugins.router_init(this); this.listen(port); this.on('error', function (err, ctx) { }); console.log('listening on port %s',config.port); };