jsdrop.

Koa starter

Middleware in the Koa style: timing, logging, and a response.

Open in editor Free. Runs in your browser, no sign-up.
  • koa
Koa

The code

Two files, exactly what you get in the editor.

import Koa from 'koa'

const app = new Koa()

// Middleware runs downstream, then back upstream.
app.use(async (ctx, next) => {
  const start = performance.now()
  await next()
  const ms = (performance.now() - start).toFixed(1)
  ctx.set('X-Response-Time', `${ms}ms`)
  console.log(`${ctx.method} ${ctx.url} ${ctx.status} ${ms}ms`)
})

app.use(async (ctx) => {
  if (ctx.path === '/api/echo') {
    ctx.body = { method: ctx.method, query: ctx.query, headers: ctx.headers }
    return
  }
  ctx.type = 'html'
  ctx.body = '<h1>Hello from Koa</h1><p>See <a href="/api/echo?hi=there">/api/echo</a>.</p>'
})

const port = Number(process.env.PORT) || 3000
app.listen(port, () => console.log(`Koa on port ${port}`))
{
  "name": "koa-server",
  "private": true,
  "type": "module",
  "dependencies": {
    "koa": "^3.0.0"
  }
}

Questions

Can I run Koa in the browser?

Yes. Press Run and the program runs in a Node.js-compatible runtime, compiled to WebAssembly, right in your browser tab. npm packages install in the browser too.

How do I start a Koa project online?

Click Open in editor. jsdrop copies this starter into a new drop with index.ts and package.json. Press Run to start it, and edit as you go.

Can other people reach my server?

Yes. When the program calls listen(), the preview shows the server and a public tunnel URL that works while your tab runs it. With Pro, deploy the drop to run around the clock at its own address.

Is it free?

Yes. Public drops, running in the browser, live collaboration, versions, and embeds are free, and you do not need an account to start.

More starter kits