简体   繁体   English

serverMiddleware 访问 Nuxt 实例或上下文

[英]serverMiddleware access Nuxt instance or context

im trying to access Nuxt runtime variables from a serverMiddleware我试图从 serverMiddleware 访问 Nuxt 运行时变量

Example i have this context.$db which i added from this plugin我有这个context.$db我从这个插件添加的例子

nuxt.config.js : nuxt.config.js


    plugins: [
        { src: '~/plugins/db_runtime.js', mode: 'server' }
    ]

~/plugins/db_runtime.js : ~/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 :服务器中间件: ~/api/index.js


    export default function(res, req) {
    }

Is there anyway to access context.$db from there?无论如何从那里访问context.$db ie IE

    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.对于 static 数据,我通常做的是使用环境变量 (.env)或在 nuxt.config.js 中声明的模块来传递数据。

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对于动态数据,由于 serverMiddleware 始终在 nuxt 应用程序的同一生命周期中被调用,因此您可以使用asyncData()fetch()axios安全地将数据作为 HTTP 请求发送

Please refer to the following link/diagram for serverMiddle lifecycle: serverMiddle生命周期请参考以下链接/图表:

Understanding modules, serverMiddleware and plugins in Nuxt.js configuration 了解 Nuxt.js 配置中的模块、serverMiddleware 和插件

So I'm not 100% versed in the Nuxt lifecycle (v2.x), but afaik you CANNOT access the Nuxt app context during the serverMiddleware lifecycle phase.所以我不是 100% 精通Nuxt 生命周期(v2.x),但据我所知,您无法在 serverMiddleware 生命周期阶段访问 Nuxt 应用程序上下文。 serverMiddleware is really connect -based (used internally by Express, btw) for processing req , res and next parameters, which represent the request, the response, and next which is connect s flow management parameter. serverMiddleware实际上是基于connect的(顺便说一句,Express 在内部使用)用于处理reqresnext参数,它们代表请求、响应和next ,这是connect的流管理参数。

The basic conceptual reason is that the Nuxt app context supports both the server and client-side, and the default 'universal' mode is a core objective of Nuxt.基本概念原因是 Nuxt 应用程序上下文同时支持服务器端和客户端,默认的“通用”模式是 Nuxt 的核心目标。 connect is really a server-side only library designed to handle the request and response nature of an application server. connect实际上是一个仅用于服务器端的库,旨在处理应用程序服务器的请求和响应特性。

Since you need config.$db , provided the configuration values you need are static, there may be an alternative way to use the nuxt.config env and the general environment variables in serverMiddleware .由于您需要config.$db ,如果您需要的配置值为 static,则可能有另一种方法使用 nuxt.config envserverMiddleware中的一般环境变量。 $config is a newer Nuxt construct intended to allow more flexible runtime environment variables (as well as normal env vars). $config是一个较新的 Nuxt 构造,旨在允许更灵活的运行时环境变量(以及正常的环境变量)。

Here is the current 2.x link to the middleware vs serverMiddleware explanation provided by Nuxt.这是 Nuxt 提供的middleware vs serverMiddleware解释的当前 2.x 链接 I've also cut/paste the text info in case the link on Nuxt decays.我还剪切/粘贴了文本信息,以防 Nuxt 上的链接失效。

serverMiddleware vs middleware!服务器中间件 vs 中间件!

Don't confuse it with routes middleware which are called before each route by Vue in Client Side or SSR.不要将它与路由middleware混淆,路由中间件在客户端或 SSR 中由 Vue 在每个路由之前调用。 Middleware listed in the serverMiddleware property runs server-side before vue-server-renderer and can be used for server specific tasks like handling API requests or serving assets. serverMiddleware属性中列出的中间件在 vue-server-renderer 之前在服务器端运行,可用于服务器特定任务,如处理 API 请求或服务资产。

Do not add serverMiddleware to the middleware/ directory.不要将serverMiddleware添加到 middleware/ 目录。

Middleware, are bundled by webpack into your production bundle and run on beforeRouteEnter.中间件,由 webpack 捆绑到您的生产包中,并在 beforeRouteEnter 上运行。 If you add serverMiddleware to the middleware/ directory it will be wrongly picked up by Nuxt as middleware and will add wrong dependencies to your bundle or generate errors.如果您将serverMiddleware添加到 middleware/ 目录,它将被 Nuxt 错误地拾取为middleware ,并将错误的依赖项添加到您的包或产生错误。

Btw, as always, if I'm incorrect on any of the above, I'm always appreciative of Stackflow users who point out what is wrong or can be explained better顺便说一句,一如既往,如果我在上述任何方面都不正确,我总是感谢指出错误或可以更好解释的 Stackflow 用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM