How to use the @koa/cors function in @koa/cors

To help you get started, we’ve selected a few @koa/cors examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github liaisonjs / liaison / resources / hosted-layer / src / test-server.js View on Github external
createServer(createLayer, _environment) {
    const server = new Koa();

    server.use(jsonError());
    server.use(cors({maxAge: 900})); // 15 minutes
    server.use(bodyParser({enableTypes: ['json'], jsonLimit: '8mb'}));

    server.use(async ctx => {
      if (ctx.method !== 'POST') {
        throw new Error('Invalid HTTP request');
      }

      const layer = await createLayer();

      const {query, items, source} = ctx.request.body;

      print(
        formatBold('→ ') +
          formatBold(formatCode(`invoke(`, {addBackticks: false})) +
          formatValue({query, items, source}, {multiline: false}) +
          formatBold(formatCode(`)`, {addBackticks: false}))
github howtocards / frontend / mock-server / server / index.js View on Github external
function createApp() {
  const app = new Koa()

  app.use(cors())
  app.use(compress())
  app.use(logger())

  // app.use(mount('/rpc', rpc))
  app.use(mount("/api", api))

  app.use(
    contentSecurityPolicy({
      directives: {
        defaultSrc: ["'self'"],
        styleSrc: ["'self'", "'unsafe-inline'"],
      },
    }),
  )

  app.use(notFoundMiddleware)
github BandoriDatabase / bangdream-data-api / src / index.js View on Github external
import { port } from './config';
import routesLoader from './utils/routesLoader';

require('./bootstrap');

const app = new Koa();
require('koa-qs')(app);

if (process.env.NODE_ENV === 'development') {
  app.use(logger());
}

app
  .use(bodyParser())
  .use(cors());

app.use(koaSwagger({
  swaggerOptions: {
    url: '/swagger.json', // example path to json
  },
}));

app.use(async (ctx, next) => {
  if (ctx.path === '/swagger.json') {
    await koaSend(ctx, ctx.path);
  } else {
    await next();
  }
});

if (!global.isFirstStart) {
github liaisonjs / liaison / packages / layer-http-server / src / layer-http-server.js View on Github external
_createKoa() {
    const koa = new Koa();

    koa.use(
      logger(message => {
        debug(message);
      })
    );
    koa.use(jsonError());
    koa.use(cors({maxAge: 900})); // 15 minutes
    koa.use(bodyParser({enableTypes: ['json'], jsonLimit: '8mb'}));

    koa.use(async ctx => {
      if (ctx.method !== 'POST') {
        throw new Error('Invalid HTTP request');
      }

      const layer = await this._layerCreator();

      try {
        const {query, items, source} = ctx.request.body;
        const result = await layer.$receiveQuery({query, items, source});
        ctx.body = {result};
      } catch (err) {
        const error = {message: err.message, ...err};
        ctx.body = {error};
github Bastiani / graphql-blog / backend / src / server.js View on Github external
import Koa from 'koa';
import Router from 'koa-router';
import graphqlHttp from 'koa-graphql';
import cors from '@koa/cors';

import connectToDatabase from './database';
import schema from './schema';

const app = new Koa();
const router = new Router();

app.use(cors());

router.all(
  '/graphql',
  graphqlHttp({
    schema,
    pretty: true,
    graphiql: true,
  }),
);

app.use(router.routes());

(async () => {
  try {
    const res = await connectToDatabase();
    console.log(res);
github hojas / vblog / server / src / app.js View on Github external
async function start() {
  await connectMongo()

  const app = new Koa()
  app.use(bodyParser())
  app.use(json())
  app.use(cors())

  const router = new Router()
  routers(router)

  app.listen(3000)
}
github apollographql / apollo-server / packages / apollo-server-koa / src / ApolloServer.ts View on Github external
},
        ),
      );
    }

    let uploadsMiddleware;
    if (this.uploadsConfig && typeof processFileUploads === 'function') {
      uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
    }

    this.graphqlPath = path;

    if (cors === true) {
      middlewares.push(middlewareFromPath(path, corsMiddleware()));
    } else if (cors !== false) {
      middlewares.push(middlewareFromPath(path, corsMiddleware(cors)));
    }

    if (bodyParserConfig === true) {
      middlewares.push(middlewareFromPath(path, bodyParser()));
    } else if (bodyParserConfig !== false) {
      middlewares.push(middlewareFromPath(path, bodyParser(bodyParserConfig)));
    }

    if (uploadsMiddleware) {
      middlewares.push(middlewareFromPath(path, uploadsMiddleware));
    }

    middlewares.push(
      middlewareFromPath(path, (ctx: Koa.Context, next: Function) => {
        if (ctx.request.method === 'OPTIONS') {
          ctx.status = 204;
github apollographql / apollo-server / packages / apollo-server-koa / src / ApolloServer.ts View on Github external
ctx.body = { status: 'pass' };
            }
          },
        ),
      );
    }

    let uploadsMiddleware;
    if (this.uploadsConfig && typeof processFileUploads === 'function') {
      uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
    }

    this.graphqlPath = path;

    if (cors === true) {
      middlewares.push(middlewareFromPath(path, corsMiddleware()));
    } else if (cors !== false) {
      middlewares.push(middlewareFromPath(path, corsMiddleware(cors)));
    }

    if (bodyParserConfig === true) {
      middlewares.push(middlewareFromPath(path, bodyParser()));
    } else if (bodyParserConfig !== false) {
      middlewares.push(middlewareFromPath(path, bodyParser(bodyParserConfig)));
    }

    if (uploadsMiddleware) {
      middlewares.push(middlewareFromPath(path, uploadsMiddleware));
    }

    middlewares.push(
      middlewareFromPath(path, (ctx: Koa.Context, next: Function) => {
github pepelatz / pepelatz-next-server / src / app.js View on Github external
import typeDefs from './schema.gql';
import resolvers from './resolvers';
import { endpointURL, isDevelopment } from './utils/config';

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: {},
  debug: isDevelopment
});

const app = new Koa();

app.use(helmet());
app.use(koaBody());
app.use(cors());

server.applyMiddleware({ app, path: endpointURL });

export default app;

@koa/cors

Cross-Origin Resource Sharing(CORS) for koa

MIT
Latest version published 1 year ago

Package Health Score

74 / 100
Full package analysis

Popular @koa/cors functions