How to use the awilix-koa.scopePerRequest function in awilix-koa

To help you get started, we’ve selected a few awilix-koa 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 mollyywang / koa2-mongodb-jwt-server / src / lib / server.js View on Github external
}).unless({
    path: [/^\/public/, "/"]
  }));

  app
    // Top middleware is the error handler.
    .use(errorHandler)
    // Compress all responses.
    .use(compress())
    // Adds ctx.ok(), ctx.notFound(), etc..
    .use(respond())
    // Parses request bodies.
    .use(bodyParser())
    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    .use(scopePerRequest(container))
    // load routes 
    .use(loadControllers('../routes/*.js', { cwd: __dirname }))

  //open database 开启数据库
  dbconnect()

  // Create http server 开启服务
  const server = http.createServer(app.callback())
  // Add a `close` event listener 监听应用关闭
  server.on('close', () => {
    // tear down database connections, etc.
    logger.debug('Server closing, bye!')
  })
  logger.debug('Server created, ready to listen', { scope: 'startup' })
  return server
}
github jeffijoe / koa-es7-boilerplate / src / lib / server.js View on Github external
// Container is configured with our services and whatnot.
  const container = (app.container = configureContainer())
  app
    // Top middleware is the error handler.
    .use(errorHandler)
    // Compress all responses.
    .use(compress())
    // Adds ctx.ok(), ctx.notFound(), etc..
    .use(respond())
    // Handles CORS.
    .use(cors())
    // Parses request bodies.
    .use(bodyParser())
    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    .use(scopePerRequest(container))
    // Create a middleware to add request-specific data to the scope.
    .use(registerContext)
    // Load routes (API "controllers")
    .use(loadControllers('../routes/*.js', { cwd: __dirname }))
    // Default handler when nothing stopped the chain.
    .use(notFoundHandler)

  // Creates a http server ready to listen.
  const server = http.createServer(app.callback())

  // Add a `close` event listener so we can clean up resources.
  server.on('close', () => {
    // You should tear down database connections, TCP connections, etc
    // here to make sure Jest's watch-mode some process management
    // tool does not release resources.
    logger.debug('Server closing, bye!')
github inkubux / MediaSpeed / src / lib / server.js View on Github external
rootDir: app.container.resolve('imageDestinationFolder')
            })
        );

    let buildFolder = process.env.NODE_ENV !== ' development' ? '/build' : '';
    app.use(
        serveStatic({
            rootPath: '/web',
            rootDir: path.join(__dirname, '/../../frontend', buildFolder),
            notFoundFile: 'index.html'
        })
    );

    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    app.use(scopePerRequest(container));

    // Load routes (API "controllers")
    app.use(loadControllers('../routes/*.js', { cwd: __dirname }));
    // Default handler when nothing stopped the chain.
    app.use(notFoundHandler);

    // Creates a http server ready to listen.
    const server = http.createServer(app.callback());

    // Add a `close` event listener so we can clean up resources.
    server.on('close', () => {
        // You should tear down database connections, TCP connections, etc
        // here to make sure Jest's watch-mode some process management
        // tool does not release resources.
        logger.debug('Server closing, bye!');
    });
github antoinechalifour / memento / src / app.ts View on Github external
export function createApp({ container }: AppOptions) {
  const app = new Koa();
  let server: Server;

  app
    .use(cors())
    .use(bodyparser())
    .use(scopePerRequest(container))
    .use(respondToRequest);

  return {
    app,
    run() {
      const config = container.resolve('config');

      return new Promise(resolve => {
        server = app.listen(config.port, resolve);
      });
    },
    stop() {
      server.close();
    },
  };
}

awilix-koa

Awilix helpers for Koa

MIT
Latest version published 2 months ago

Package Health Score

64 / 100
Full package analysis

Similar packages