I have a koa 2 server.
The following code are my middlewares:
// parse body
app.use( bodyParser() )
// serve static
app.use( serve( path.join(__dirname, '/public') ) )
// routes
app.use( routes )
// error middleware
app.use( async ctx => ctx.throw(500) )
Everything works well but my problem is that when I go to localhost:8000, where my server lives, in the console I see the following error:
InternalServerError: Internal Server Error at Object.throw (/Users/work/Desktop/server/node_modules/koa/lib/context.js:91:23)
I'm suspecting that after static, the app is going to the next middleware, which is the error middleware.
PS. I'm using app.use( async ctx => ctx.throw(500) ), to call next() if I'm getting errors on the other routes.
Does anyone know how to fix this?
Thanks!
koa-statictransfers control to the next middleware by design. Yourroutesmiddleware alsoawaitto the next middleware. So you get an error.It's hard to say what you are going to achieve in the first place. Setting 500 manually is probably a wrong idea. There should be 404 like:
For SPA (without SSR) you probably want this catch-all route to send APP layout instead. And move that
404middleware to the beginning of the file (where it will take control on the second "bubbling" phase.Make sure you checked this