Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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}))
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)
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) {
_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};
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);
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)
}
},
),
);
}
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;
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) => {
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;