How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • app.use(async ctx => {
      ctx; // is the Context
      ctx.request; // is a Koa Request
      ctx.response; // is a Koa Response
    });
    Docs
  • const Koa = require('koa')
    const rTracer = require('cls-rtracer')
    
    const app = new Koa()
    // any third party middleware that does not need access to request ids goes here
    // ...
    
    app.use(rTracer.koaMiddleware())
    // optionally, you can override default middleware config:
    // app.use(rTracer.koaMiddleware({
    //   useHeader: true,
    //   headerName: 'X-Your-Request-Header'
    // }))
    
    // all code in middlewares, starting from here, has access to request ids
    Docs
  • var Koa = require('koa');
    var bodyParser = require('koa-bodyparser');
     
    var app = new Koa();
    app.use(bodyParser());
     
    app.use(async ctx => {
      // the parsed body will store in ctx.request.body
    // the parsed body will store in ctx.request.body
      // if nothing was parsed, body will be an empty object {}
    // if nothing was parsed, body will be an empty object {}
      ctx.body = ctx.request.body;
    });
    Docs
  • import { ApolloServer } from 'apollo-server-koa';
    import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
    import Koa from 'koa';
    import http from 'http';
    
    async function startApolloServer(typeDefs, resolvers) {
      const httpServer = http.createServer();
      const server = new ApolloServer({
        typeDefs,
        resolvers,
        plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
      });
    
      await server.start();
      const app = new Koa();
      server.applyMiddleware({ app });
      httpServer.on('request', app.callback());
      await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
      console.log(`ūüöÄ Server ready at http://localhost:4000${server.graphqlPath}`);
      return { server, app };
    }
    Docs
  • import * as MediaLibrary from 'expo-media-library';
    import * as FileSystem from 'expo-file-system';
    const { StorageAccessFramework } = FileSystem;
    
    async function migrateAlbum(albumName: string) {
      // Gets SAF URI to the album
      const albumUri = StorageAccessFramework.getUriForDirectoryInRoot(albumName);
    
      // Requests permissions
      const permissions = await StorageAccessFramework.requestDirectoryPermissionsAsync(albumUri);
      if (!permissions.granted) {
        return;
      }
    
      const permittedUri = permissions.directoryUri;
      // Checks if users selected the correct folder
      if (!permittedUri.includes(albumName)) {
        return;
      }
    
      const mediaLibraryPermissions = await MediaLibrary.requestPermissionsAsync();
      if (!mediaLibraryPermissions.granted) {
        return;
      }
    
      // Moves files from external storage to internal storage
      await StorageAccessFramework.moveAsync({
        from: permittedUri,
        to: FileSystem.documentDirectory!,
      });
    
      const outputDir = FileSystem.documentDirectory! + albumName;
      const migratedFiles = await FileSystem.readDirectoryAsync(outputDir);
    
      // Creates assets from local files
      const [newAlbumCreator, ...assets] = await Promise.all(
        migratedFiles.map<Promise<MediaLibrary.Asset>>(
          async fileName => await MediaLibrary.createAssetAsync(outputDir + '/' + fileName)
        )
      );
    
      // Album was empty
      if (!newAlbumCreator) {
        return;
      }
    
      // Creates a new album in the scoped directory
      const newAlbum = await MediaLibrary.createAlbumAsync(albumName, newAlbumCreator, false);
      if (assets.length) {
        await MediaLibrary.addAssetsToAlbumAsync(assets, newAlbum, false);
      }
    }
    
    Docs
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples