Skip to content

Commit

Permalink
Force h3 to send the correct content-type for JSON (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 16, 2024
1 parent 0ec3537 commit 31bbeef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions benchmarks/h3-router.cjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'use strict'

const { createServer } = require('node:http')
const { createApp, toNodeListener, eventHandler, createRouter } = require('h3')
const { createApp, toNodeListener, eventHandler, createRouter, setHeader } = require('h3')

const app = createApp()

const router = createRouter()
.get('/', eventHandler(() => ({ hello: 'world' })))
.get('/', eventHandler((ev) => {
// Unfortunatly, we need to set the content-type manually
// to level the paying field
setHeader(ev, 'content-type', 'application/json; charset=utf-8')
return { hello: 'world' }
}))

app.use(router)

Expand Down
9 changes: 7 additions & 2 deletions benchmarks/h3.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict'

const { createServer } = require('node:http')
const { createApp, toNodeListener, eventHandler } = require('h3')
const { createApp, toNodeListener, eventHandler, setHeader } = require('h3')

const app = createApp()
app.use('/', eventHandler(() => ({ hello: 'world' })))
app.use('/', eventHandler((ev) => {
// Unfortunatly, we need to set the content-type manually
// to level the paying field
setHeader(ev, 'content-type', 'application/json; charset=utf-8')
return { hello: 'world' }
}))

createServer(toNodeListener(app)).listen(process.env.PORT || 3000)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"express": "^4.18.2",
"fastify": "^4.10.2",
"frameguard": "^4.0.0",
"h3": "^1.0.2",
"h3": "^1.10.0",
"hide-powered-by": "^1.1.0",
"hono": "^3.10.3",
"hsts": "^2.2.0",
Expand Down

0 comments on commit 31bbeef

Please sign in to comment.