Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use it in koa2? #29

Closed
ltjin opened this issue Oct 13, 2019 · 12 comments · Fixed by #285
Closed

How to use it in koa2? #29

ltjin opened this issue Oct 13, 2019 · 12 comments · Fixed by #285

Comments

@ltjin
Copy link

ltjin commented Oct 13, 2019

I want to use it in an application built by koa2,but I don't get it.
my code like:

const Koa = require('koa');
const app = new Koa();
const { UI } = require('bull-board');

app.use((ctx, next) => {
    if(ctx.path.startWith('/monitor')) {
        if (ctx.status === 404 || ctx.status === '404') {
            delete ctx.res.statusCode
        }
        ctx.respond = false;
        UI(ctx.req, ctx.res);
    } else {
        next();
    }
});

This is from Can we mount express app into koa?

@vcapretz
Copy link
Contributor

hey @ltjin! thank's for the issue!

I didn't really consider this possibility 😅so maybe there are some gotchas on how would this be setup.
I'll try to reproduce your code in a small app and let you know what I find out!

@roparz
Copy link

roparz commented Dec 11, 2019

Hi,

This worked for me, with Koa router:

const Router = require('@koa/router')
const { UI } = require('bull-board')
const expressApp = require('express')()

expressApp.use('/monitor', UI)

const router = new Router()

router.all('/monitor*', ctx => {
  if (ctx.status === 404 || ctx.status === '404') {
    delete ctx.res.statusCode
  }
  ctx.respond = false
  expressApp(ctx.req, ctx.res)
})

EDIT: use router.all instead of router.get

@ltjin
Copy link
Author

ltjin commented Dec 11, 2019

@roparz yes, that's right.
I also found a way:

const router = require('koa-router')();
const { UI } = require('bull-board');

router.prefix('/monitor');

router.all('*', async (ctx, next) => {
    if (ctx.status === 404 || ctx.status === '404') {
        delete ctx.res.statusCode
    }
    ctx.respond = false;
    const app = UI('/monitor');   // add routing prefix to express app
    app(ctx.req, ctx.res);
});

module.exports = router;

and bull-board/index.js

const Queue = require('bull')
const express = require('express')
const bodyParser = require('body-parser')
const router = require('express-async-router').AsyncRouter()
const path = require('path')

const queues = {}

function UI(prefix) {     // add routing prefix to express app
  const app = express()

  app.locals.queues = queues

  app.set('view engine', 'ejs')
  app.set('views', `${__dirname}/ui`);

  router.use('/static', express.static(path.join(__dirname, './static')))
  router.get('/queues', require('./routes/queues'))
  router.put('/queues/:queueName/retry', require('./routes/retryAll'))
  router.put('/queues/:queueName/:id/retry', require('./routes/retryJob'))
  router.put('/queues/:queueName/clean', require('./routes/cleanAll'))
  router.get('/', require('./routes/index'))

  app.use(bodyParser.json())
  if(prefix){ // add routing prefix to express app
    app.use(prefix, router)
  }else{
    app.use(router)
  }
  

  return app
}

module.exports = {
  UI: UI,  // return UI
  createQueues: redis => {
    return {
      add: (name, opts) => {
        const queue = new Queue(name, redis, opts)
        queues[name] = queue

        return queue
      }
    }
  },
  get: ()=>{
    return queues;
  }
}

This way makes intrusive changes to the source code, I'm not sure if I should submit a PR

@roparz
Copy link

roparz commented Dec 11, 2019

You make me point out that my solution only works on GET requests...

@ltjin
Copy link
Author

ltjin commented Dec 11, 2019

The core of this problem is that the request link forwarded by Koa does not have a corresponding route in the expressAPP inside the bull-board.
So it is key for the bull-board to recognize all the forwarded routes.

@vcapretz
Copy link
Contributor

Hey folks, nice that you took the time to think about a solution, I really appreciate it since I didn’t have the time to look into it so far 😅

Please feel free to open a PR, I’ll review it properly and as long as we don’t introduce breaking changes I guess it's fine 👍

@Janealter
Copy link

Janealter commented Jun 4, 2020

@vcapretz please add prefix support (something like what @ltjin wrote) in version 1.0.0 😊 This will allow us to use some other servers, not just Express.

@eric-hc
Copy link

eric-hc commented Feb 24, 2021

Has anyone solved this with recent versions of bull-board?

@felixmosh
Copy link
Owner

felixmosh commented May 29, 2021

Since v3.0.0 of bull-board it is possible to add server adapters pretty simple, I didn't prepared a koa adapter since I'm not familiar with it.

PR's are always welcome.

@Israel002
Copy link

@felixmosh I cannot install v3.0.0 the highest on npm is 2.1.3. Please can you push the latest version to npm. Thanks in advance.

@felixmosh
Copy link
Owner

Pay attention to the fact that we started to release under @bull-board scope ;)

@Israel002
Copy link

Oh, I didn't notice that. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants