Cyan
Cyan4w ago

koa-router behaves different in Deno

Trying to run following script
import Koa from 'koa/lib/application';
import KoaRouter from '@koa/router';

const app = new Koa({});
const router = new KoaRouter();

app.use(router.routes())
router.get("/", (ctx, next) => {
ctx.type = "text/javascript"
ctx.body = "console.log('hello')"
})

app.listen(2000)
import Koa from 'koa/lib/application';
import KoaRouter from '@koa/router';

const app = new Koa({});
const router = new KoaRouter();

app.use(router.routes())
router.get("/", (ctx, next) => {
ctx.type = "text/javascript"
ctx.body = "console.log('hello')"
})

app.listen(2000)
With Node.js, request http://127.0.0.1:2000/ gives me console.log('hello') with Content-Type set to text/javascript. But with Deno, request the same url, gives console.log('hello'), but the Content-Type is set to text/plain. Then, your browser will refuse to execute the script if the Content-Type is not correct. And everything is broken. I can't simply switch to Oakserver, this is just a minimal repro. In fact, the actual project is so large and coupled with Koa deeply. Also ctx.type works correctly without koa-router. I don't know what is going on underlying koa-router
2 Replies
marvinh.
marvinh.4w ago
This was fixed with https://github.com/denoland/deno/pull/27105 . The fix was included in Deno 2.1.2 To resolve this issue update your Deno version to 2.1.2 by running deno upgrade
Cyan
CyanOP4w ago
I'll try it tomorrow. Thanks I can confirmed it's fixed!

Did you find this page helpful?