im trying to access Nuxt runtime variables from a serverMiddleware
Example i have this context.$db which i added from this plugin
nuxt.config.js:
plugins: [
{ src: '~/plugins/db_runtime.js', mode: 'server' }
]
~/plugins/db_runtime.js:
db = 'test'
export default ({ app }, inject) => {
inject('db', db)
}
Then i added a serverMiddleware:
serverMiddleware: [
{ path: '/api', handler: '~/api/index.js' },
],
The serverMiddleware: ~/api/index.js:
export default function(res, req) {
}
Is there anyway to access context.$db from there? i.e
export default function(res, req) {
$config.db = null
}
For static data, what i usually did is using either environment variables (.env) or modules declared in nuxt.config.js to pass the data.
For dynamic data, since serverMiddleware is always invoked in the same lifecycle of your nuxt app, you can safely send the data as HTTP request using asyncData() or fetch() or axios
Please refer to the following link/diagram for serverMiddle lifecycle:
Understanding modules, serverMiddleware and plugins in Nuxt.js configuration