framework icon indicating copy to clipboard operation
framework copied to clipboard

nitroApp.h3App.use() isn't working

Open modelair opened this issue 2 years ago • 8 comments

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

  1. Create a nuxt3 app
  2. Create a nitro plugin
  3. 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({}))
}

modelair avatar Aug 11 '22 00:08 modelair

Maybe you should use https://nitro.unjs.io/guide/storage.html#defining-mountpoints

HomWang avatar Aug 11 '22 06:08 HomWang

@516310460 not looks like solution

modelair avatar Aug 11 '22 09:08 modelair

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

HomWang avatar Aug 11 '22 09:08 HomWang

@danielroe @pi0 nitro is it experimental

HomWang avatar Aug 11 '22 09:08 HomWang

@modelair See link: https://github.com/nuxt/framework/pull/6507

HomWang avatar Aug 11 '22 09:08 HomWang

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).

danielroe avatar Aug 11 '22 09:08 danielroe

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

HomWang avatar Aug 11 '22 09:08 HomWang