Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Oct 26 22:16
    miwnwski commented #1511
  • Oct 26 04:38
    jayli edited #1511
  • Oct 26 04:37
    jayli edited #1511
  • Oct 26 04:36
    jayli edited #1511
  • Oct 26 04:36
    jayli edited #1511
  • Oct 26 04:34
    jayli opened #1511
  • Oct 25 17:41
    madarche opened #1510
  • Oct 24 19:01
    ctrlaltdylan commented #1505
  • Oct 24 18:46
    Israel001 commented #1505
  • Oct 24 18:36
    ctrlaltdylan commented #1505
  • Oct 24 18:34
    Israel001 commented #1505
  • Oct 24 18:01
    ctrlaltdylan commented #1505
  • Oct 22 14:40
    X-neuron commented #1114
  • Oct 21 09:18
    iedmrc commented #881
  • Oct 13 11:13
    joelwallis commented #1509
  • Oct 11 13:18
    miwnwski commented #1509
  • Oct 11 13:17
    miwnwski commented #1509
  • Oct 11 13:17
    miwnwski closed #1509
  • Oct 11 13:17
    miwnwski commented #1509
  • Oct 10 18:25
    joelwallis commented #1509
Shashank Agarwal
@shashank1010

I intend to create an application and reuse some app logic by forwarding an API call from one route to another.

for example. A request is made for /subscribed-users and i want to add to this context and call the /users route internally.

How can i achieve this with KOA?
Any help would be welcome.

Thanks.

Mayurs
@Marvin9
@shashank1010 , ctx.redirect should be used for this purpose. Read the documentation here https://github.com/koajs/koa/blob/master/docs/api/response.md#responseredirecturl-alt.
zavr-1
@zavr-1
hey i wanted to share my article https://medium.com/@art_deco/how-i-compiled-koa-with-closure-63c064d7fa36 maybe someone can find it useful
djazz
@daniel-j
Hi! What does the name koa mean?
djazz
@daniel-j
I'm guessing it comes from Co-routines
Freddy
@fjeddy
Mr Swedish god @jeffijoe, are you alive?
Jeff Hansen
@jeffijoe
@fjeddy I'm danish lol
Freddy
@fjeddy
Sorry, forgot :P
function Property {
  this.something = 'Something'
}

Property.prototype.test = require( './test.js' )

module.exports = Property
That there, the test file will have access to the context right?
As in, this.something will exist in Property.test
But, if I want to do multiple levels, I want to add more files inside the test file so I get Property.test.a and still persist the this context
Jeff Hansen
@jeffijoe
If invoked with the right context, it should, yeah.
Freddy
@fjeddy
Is that possible?
Jeff Hansen
@jeffijoe
I would just not do that.
Freddy
@fjeddy
Why?
The idea is to split one large class file into multiple files
Jeff Hansen
@jeffijoe
I mean you can just iterate those props and bind the funcs in the ctor
What are you trying to achieve?
Freddy
@fjeddy
And because the functions are so large, i would prefer to have one method in one file
My constructor() contains quite a lot of code that is used by almost every single function
So my goal is to split the entire class file into multiple files, but keep the idea of the constructor, and having the data the constructor defined, available in all the sub files
Jeff Hansen
@jeffijoe
Why do you want multiple levels?
Freddy
@fjeddy
Better structure
Keeping one method in one file, as they are rather long
Jeff Hansen
@jeffijoe
Have you considered mixins?
Freddy
@fjeddy
I've looked at it, from what I understood most of them requires a library?
Jeff Hansen
@jeffijoe
Have your sub-files export a function that you call with the prototype
Lol no, module.exports = function testMixin(proto) { proto.test = function() {}; return proto }
Dylan Pierce
@ctrlaltdylan

Just curious on middleware - I'm trying to initialize my mongodb connection in middleware and share it as ctx.db.

When you pass an async function like app.use(async (ctx, next) => { }) does the middleware get resolved before the request is processed?

Jame
@jamemackson
you’ll probably want to initialize your db connection when your app initialized and not in middleware which fire for each connection.
i’d reread the docs about middleware
Dylan Pierce
@ctrlaltdylan
I think I'll have to make a singleton on initialization to represent the database pool and then pass it to the middleware
so it's not reinitializing the db connection on every request
Jame
@jamemackson
that sounds like a solid plan. we’re using TypeORM and thankfully they handle that for us but I believe is their approach as well.
gcnyin
@gcnyin
Hi guys, How do you do distributed trace in koa/express? I found a node.js api AsyncLocationStorage in async_hooks module. I think it's easy to create tracing middleware base on this api. This is my demo repo https://github.com/gcnyin/koa-trace-id. Core part is
const app = new Koa();
const traceIdStorage = new AsyncLocalStorage<string>();

app.use(async (ctx, next) => {
    const traceId = ctx.request.header['x-trace-id'] ?? uuid4()
    traceIdStorage.run(traceId, next)
})

app.use(async ctx => {
    console.log('start', traceIdStorage.getStore())
    await new Promise<void>((resolve) => setTimeout(resolve, 100))
    console.log('end', traceIdStorage.getStore())
    ctx.body = 'Hello World'
})
This is just for application internal. For micro services, you can rewrite http/https modules, and inject traceId in header. These are my thoughts. End.
gcnyin
@gcnyin
Oh, I found cls-rtracer. It already did it.
githubabusebygitter
@githubabusebygitter
When I ctx.throw(), my logger displays the error I throw, but my browser receives a generic 500, Internal Server Error
Something is stealing my Error between the logger (first/last middleware) and the browser
nginx proxy is configured not to intercept errors
any suggestion where to look?
Ivan Jeremic
@ivanjeremic
Hey all. Is there any chance Koa will overtake Express at any time in usage? Why are people so holding on Express?
githubabusebygitter
@githubabusebygitter
@ivanjeremic did three days of silence help answer your question?
Ivan Jeremic
@ivanjeremic
@githubabusebygitter LoL yes, makes no sense to use Koa with this community
Mohammad Hossein Mardani
@mhmardani
hi, guys, I'm node js developer with over 5 years experience and now I'm looking for a development team to join them for work on startups, open-source projects, is anyone here to accept me :)
Davide Pugliese
@Deviad
Hello, I have created a generic function to handle errors,
unfortunately the execution does not stop at the error,
but it keeps going.
https://gist.github.com/Deviad/3b5f9421f791fc5ca790458a57c94e34?fbclid=IwAR1Omdz74gXqI5VcMzx0k5VLi66mlLClZBcTAuTBrarToccErEx9pZ7B9D0#file-handleerrors-ts
What should I do to stop the execution and return an error as I receive it?
Davide Pugliese
@Deviad
Hello
How can I make this work:
app.on('error', (err, ctx) => {
ctx.body = {
data: {
message: err.message,
status: 'error'
}
};
}); ?
1 reply
Jumping Through Hoops
@jumpingthroughhoops_gitlab
how does koa determine mimetype of static files?