本文整理匯總了TypeScript中@koa/cors.default函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript default函數的具體用法?TypeScript default怎麽用?TypeScript default使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了default函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: require
import * as Koa from 'koa';
import * as Router from 'koa-router';
import * as multer from 'koa-multer';
import * as bodyParser from 'koa-bodyparser';
const cors = require('@koa/cors');
import endpoints from './endpoints';
const handler = require('./api-handler').default;
// Init app
const app = new Koa();
app.use(cors({
origin: '*'
}));
app.use(bodyParser({
// リクエストが multipart/form-data でない限りはJSONだと見なす
detectJSON: ctx => !ctx.is('multipart/form-data')
}));
// Init multer instance
const upload = multer({
storage: multer.diskStorage({})
});
// Init router
const router = new Router();
示例2: Application
constructor() {
this.app = new Application();
this.app.use(koaLogger());
this.app.use(koaResponseTime());
this.app.use(koaBodyParser({ onerror: handleParseError }));
this.app.use(koaErrorHandler());
this.app.use(koaRestHandler());
this.app.use(koaAuth());
this.app.use(koaCors({ exposeHeaders, allowHeaders, origin, maxAge: 600, credentials: true, keepHeadersOnError: true }));
this.app.use(koaJson({ pretty: false, param: 'pretty' }));
this.app.use(apiCache.middleware.bind(apiCache));
/* istanbul ignore next: host website at the same time, currently used for website CI */
if (process.env.WEBAPP) {
const webappPath = resolve(__dirname, process.env.WEBAPP);
if (existsSync(webappPath)) {
logger.warn(null, '[Server] Statically hosting website at %s', webappPath);
this.app.use(koaWebsiteHandler(webappPath));
} else {
logger.warn(null, '[Server] Fix env WEBAPP, nothing found at %s (%s)', webappPath, process.env.WEBAPP);
}
}
/* istanbul ignore if */
if (process.env.ELASTIC_APM_ENABLED) {
logger.info(null, '[Server] Elastic application performance monitoring at %s enabled.', process.env.ELASTIC_APM_SERVER_URL);
this.setupApmFilter();
}
}
示例3: ServerService
export const ServerServiceFactory = (sm: ServiceManagerInterface) => {
const config = sm.get(Config).of<ServerConfigInterface>('server');
const middleware = [
sm.get(RequestMiddleware),
bodyParser(),
sm.get(RouterMiddleware),
sm.get(DispatchMiddleware),
];
if (config.cors.enabled) {
middleware.unshift(cors(config.cors.options));
}
return new ServerService(sm.get(Application).getMode(), config, middleware);
};
示例4: applyMiddleware
// TODO: While Koa is Promise-aware, this API hasn't been historically, even
// though other integration's (e.g. Hapi) implementations of this method
// are `async`. Therefore, this should become `async` in a major release in
// order to align the API with other integrations.
public applyMiddleware({
app,
path,
cors,
bodyParserConfig,
disableHealthCheck,
onHealthCheck,
}: ServerRegistration) {
if (!path) path = '/graphql';
// Despite the fact that this `applyMiddleware` function is `async` in
// other integrations (e.g. Hapi), currently it is not for Koa (@here).
// That should change in a future version, but that would be a breaking
// change right now (see comment above this method's declaration above).
//
// That said, we do need to await the `willStart` lifecycle event which
// can perform work prior to serving a request. While we could do this
// via awaiting in a Koa middleware, well kick off `willStart` right away,
// so hopefully it'll finish before the first request comes in. We won't
// call `next` until it's ready, which will effectively yield until that
// work has finished. Any errors will be surfaced to Koa through its own
// native Promise-catching facilities.
const promiseWillStart = this.willStart();
app.use(
middlewareFromPath(path, async (_ctx: Koa.Context, next: Function) => {
await promiseWillStart;
return next();
}),
);
if (!disableHealthCheck) {
app.use(
middlewareFromPath(
'/.well-known/apollo/server-health',
(ctx: Koa.Context) => {
// Response follows https://tools.ietf.org/html/draft-inadarei-api-health-check-01
ctx.set('Content-Type', 'application/health+json');
if (onHealthCheck) {
return onHealthCheck(ctx)
.then(() => {
ctx.body = { status: 'pass' };
})
.catch(() => {
ctx.status = 503;
ctx.body = { status: 'fail' };
});
} else {
ctx.body = { status: 'pass' };
}
},
),
);
}
let uploadsMiddleware;
if (this.uploadsConfig && typeof processFileUploads === 'function') {
uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
}
this.graphqlPath = path;
if (cors === true) {
app.use(middlewareFromPath(path, corsMiddleware()));
} else if (cors !== false) {
app.use(middlewareFromPath(path, corsMiddleware(cors)));
}
if (bodyParserConfig === true) {
app.use(middlewareFromPath(path, bodyParser()));
} else if (bodyParserConfig !== false) {
app.use(middlewareFromPath(path, bodyParser(bodyParserConfig)));
}
if (uploadsMiddleware) {
app.use(middlewareFromPath(path, uploadsMiddleware));
}
app.use(
middlewareFromPath(path, (ctx: Koa.Context, next: Function) => {
if (ctx.request.method === 'OPTIONS') {
ctx.status = 204;
ctx.body = '';
return;
}
if (this.playgroundOptions && ctx.request.method === 'GET') {
// perform more expensive content-type check only if necessary
const accept = accepts(ctx.req);
const types = accept.types() as string[];
const prefersHTML =
types.find(
(x: string) => x === 'text/html' || x === 'application/json',
) === 'text/html';
if (prefersHTML) {
//.........這裏部分代碼省略.........