framework
framework copied to clipboard
nitroApp.h3App.use() isn't working
Environment
Nuxt project info: 11:41:02
- Operating System:
Darwin
- Node Version:
v16.15.1
- Nuxt Version:
3.0.0-rc.6
- Package Manager:
[email protected]
- Builder:
vite
Reproduction
- Create a nuxt3 app
- Create a nitro plugin
- Import it in nuxt.config.ts
// ~/server/nitro/plug.ts
export default (nitroApp) => {
nitroApp.h3App.use((event) => console.log('hello nitro')) // logs nothing
}
// nuxt.config.ts
export default defineNuxtConfig({
nitro: {
plugins: ['~/server/nitro/plug.ts']
}
})
Describe the bug
Nitro plugin h3App.use() isn't works
Additional context
But simple h3app works!
import { createApp } from 'h3'
import { createServer } from 'http'
const app = createApp()
createServer(app).listen(process.env.PORT || 3000)
app.use((event) => console.log('hello')) // logs 'hello'
I found this when i want try to use express-session
with h3 and nuxt3.
import session from 'express-session'
export default async (_nitroApp) => {
_nitroApp.h3App.use(session({}))
}
Maybe you should use https://nitro.unjs.io/guide/storage.html#defining-mountpoints
@516310460 not looks like solution
There is no such document now, but I am not sure whether this function is experimental. I will submit a document later to see if it will be included
@danielroe @pi0
nitro
is it experimental
@modelair See link: https://github.com/nuxt/framework/pull/6507
The issue is that you are adding a route after a catchall middleware (nuxt rendering) so it will never be hit.
This is similar to https://github.com/nuxt/framework/issues/5527 and perhaps there is a use case for adding middleware in the same way that you can now to nitroApp.router.use('/some/route', handler)
.
The issue is that you are adding a route after a catchall middleware (nuxt rendering) so it will never be hit.
This is similar to #5527 and perhaps there is a use case for adding middleware in the same way that you can now to
nitroApp.router.use('/some/route', handler)
.
nitro is it experimental