Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 触发 koa 统一错误事件,可以打印出详细的错误堆栈 log
app.emit('error', e, ctx);
}
}
});
app.use(conditional());
// cache and static resource
app.use(koaStatic('static', {
maxage: 1000 * 60 * 60 * 24 * 30,
}));
// etag middleware
app.use(koaEtag());
// template ejs
app.use(views(templatePath, { extension: 'ejs' }));
// router dispatcher
app.use(controller.routes());
// 404
app.use(async() => {
throw {status: 404};
});
}
export default function init() {
// global middlewares
app.keys = ['sugo:' + env]
app.use(compress({
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}))
//get
app.use(conditional())
// add etags
app.use(etag())
// //static
// app.use(async (ctx, next) => {
// ctx.set('Cache-Control', 'private, no-cache, no-store, must-revalidate')
// ctx.set('Expires', '-1')
// ctx.set('Pragma', 'no-cache')
// await next()
// })
app.use(mount('/_bc', serve(cwd + '/node_modules', staticOption())))
// body
app.use(bodyparser)
if (env === 'development') {
app.use(logger())
}
this.body = error.message;
this.app.emit('error', error, this);
}
});
// gzip compression
app.use(gzip());
// Conditional GET
app.use(conditional());
// Freshness testing
app.use(fresh());
// etags
app.use(etag());
// Serve static assets from `public` directory
app.use(serve('public'));
// Add jade rendering
app.use(views(path.join(process.cwd(), 'views'), {
cache: true,
default: 'jade',
}));
app.use(router(app));
}
export const initialLayer = (app: Object) =>
app
.use(bodyParser())
.use(conditionalGet())
.use(etag()); // https://github.com/koajs/bodyparser // https://github.com/koajs/conditional-get // https://github.com/koajs/etag
apiApp.use(passport.initialize());
apiApp.use(passport.session());
apiApp.use(demoAuth);
apiApp.use(api.routes());
apiApp.use(api.allowedMethods());
const app = new Koa();
app.keys = [ config.sessionSecret ];
app.use(errorHandler);
if (config.basicAuthUser) {
const basicAuth = auth({ name: config.basicAuthUser, pass: config.basicAuthPassword });
app.use(basicAuth);
}
app.use(mount('/api', apiApp));
app.use(conditional());
app.use(etag());
app.use(compress());
app.use(mount('/', koaStatic(__dirname + '/../client')));
app.use(async (ctx) => {
// return index.html for anything that is not handled
ctx.body = await Promise.fromCallback(cb => fs.readFile(__dirname + '/../client/index.html', 'utf-8', cb));
});
export default app;
const app = new Koa();
/** @type {import('chokidar').FSWatcher | null} */
const fileWatcher = watch ? chokidar.watch([]) : null;
// add custom user's middlewares
if (customMiddlewares && customMiddlewares.length > 0) {
customMiddlewares.forEach(customMiddleware => {
app.use(customMiddleware);
});
}
// serves 304 responses if resource hasn't changed
app.use(createCacheMiddleware());
// adds etag headers for caching
app.use(koaEtag());
// communicate with browser for reload or logging
if (setupMessageChanel) {
app.use(createMessageChannelMiddleware({ rootDir, appIndex }));
}
// watch files and reload browser
if (watch) {
app.use(
createReloadBrowserMiddleware({
rootDir,
fileWatcher,
watchExcludes,
watchDebounce,
}),
);
exposableErrorTypes: [
CHECK_ERROR_TYPE,
GITHUB_ERROR_TYPE,
REPO_ERROR_TYPE
]
}))
.use(morgan(morganFormat, {skip: morganSkip}))
.use(convert(session({store: store})))
.use(bodyParser())
.use(passport.initialize())
.use(passport.session())
.use(compress())
.use(router.routes())
.use(router.allowedMethods())
.use(conditional())
.use(etag())
.use(serve(
nconf.get('STATIC_DIR'), {
index: 'none',
maxage: 1.7 * 10 ** 8 // ~ 2 days
}))
.use(ensureModeMiddleware)
.use(renderStatic)
}
module.exports = function (app) {
// middleware configuration
app.use(responseTime);
app.use(cors());
app.use(conditional());
app.use(etag());
app.use(bodyParser());
app.use(authenticate);
app.use(router(app));
app.use(logger());
// create all models first so controllers have them available
let model, schema;
for (let name of require('fs').readdirSync(__dirname+'/../models')) {
if (name[0] === '.') return;
name = name.substring(0, name.length - 3);
schema = require('../models/' + name);
model = mongoose.model(name, schema);
};
// auto mount all the simple routes defined in the api controllers
// initialize complex custom defined routes
export default function startEtagsMiddleware(application) {
application.engine.use(conditional());
application.engine.use(function* test(next) {
yield next;
});
return etag();
}
}
if (logger) {
this.use(logger())
}
if (app.helmet !== false) {
this.use(helmet())
}
if (app.cors !== false) {
this.use(cors(isObject(app.cors) ? app.cors : {}))
}
if (app.compress !== false) {
this.use(compress(app.compress))
}
if (app.etag !== false) {
this.use(conditional())
this.use(etag())
}
}