How to transfer data from Nuxt (universal) serverMiddleware "up" to display on the page?

Good afternoon.

Made custom middleware on the server side for example https://nuxtjs.org/api/configuration-servermiddleware/ and have a few questions:

1. Let's say I generated the data inside the middleware - how do I now display on the browser page? How to pass higher?

2. Tried using $store (Vuex) - but nothing came of it. I understand that I do not understand something basic here, but what exactly - I don't know))

3. Why in my middleware (in the example file api/logger.js) variable
process.server == undefined
why is this? Googled it, people wrote that they like the work ...but it's not sure))

Prompt in what side to dig, or maybe a link to an example.

Och grateful in advance.
March 23rd 20 at 19:28
1 answer
March 23rd 20 at 19:30
There are 2 kinds of middleware is simply middleware and serverMiddleware.

Conventional middleware is for vue router, both on the client and on the server, i.e., work only for vue serverMiddleware to Express that we have under the hood initially.
But first, Express and serverMiddleware(if you have), and then vue and its router.

From serverMiddleware nothing can be passed in vue and similar modules, as the vue is not initialized yet, so to speak.
Of middleware you can pass the data in the warehouse vue

process.server only works when the content generates on the server. If you have it undefined, then the variable worked on the client, and to use the process.server serverMiddleware or not(I haven't checked), or meaningless, because it and so the server and only the server
From serverMiddleware nothing can be passed in vue and similar modules, as the vue is not initialized yet, so to speak.


Here is the number!))

And how to be in this situation?

For example, I initialize in serverMiddleware (there is no way of Express and Connect - the ancestor of Express))) any data

var mydata = "my data";

I can now display them on the page? somehow can?

otherwise what's the point to do in the installation options Nuxt select server (Express, Koa, etc.) then if it is not used? - Eunice_Prohaska8 commented on March 23rd 20 at 19:33
@Eunice_Prohaska8, weird about Connect, because the dependencies explicitly Express, and in dock the example of the expression, but whatever.

otherwise what's the point to do in the installation options Nuxt select server (Express, Koa, etc.) then if it is not used?


To write the backend on the node.js. You have a rest api. You serverMiddleware have to put data into the database, and from the vue to demand it. Directly so impossible to pass. You simply do not have access to the vue. Or, for small values use cookies - Glenda.Lemke22 commented on March 23rd 20 at 19:36
@Glenda.Lemke22, and unless I have Vue have access to the database?

I installed the module mysql and with his help and can work with database from serverMiddleware

But what is the point, if I can't pass the result of the query in Vue. - Eunice_Prohaska8 commented on March 23rd 20 at 19:39
@Eunice_Prohaska8, well, because it doesn't work as you want. the rest api is the separation of front and Beck. And they communicate by means of ajax requests and do not intersect anywhere else. In serverMiddleware you put data into the database, and at the same from the front send the request to the handler at the url /api/users/ to get data and output it - Glenda.Lemke22 commented on March 23rd 20 at 19:42
and at the same from the front send the request to the handler at the url /api/users/ to get data and output it


So as to bring them what? The same serverMiddleware?

See:

- let's say I send a request to /api/users/ - and who accepts it? Express same? That is, the request will be processed using serverMiddleware?

Somehow I'm confused :)

...that is, I in any case have to give JSON with res.send() and no other options here? - Eunice_Prohaska8 commented on March 23rd 20 at 19:45
let's say I send a request to /api/users/ - and who accepts it? Express same? That is, the request will be processed using serverMiddleware?
Yes. It will handle and should return you the result. Sent a get request, then get the data sent post request, then write the data into the database

...that is, I in any case have to give JSON with res.send() and no other options here?
so - Glenda.Lemke22 commented on March 23rd 20 at 19:48
@Glenda.Lemke22, pechalka))

In any case, thank you very much! - Eunice_Prohaska8 commented on March 23rd 20 at 19:51
@Eunice_Prohaska8, I was glad to help))
https://t.me/nuxtjs_ru telegrams channel Russian-speaking users nuxt.js - Glenda.Lemke22 commented on March 23rd 20 at 19:54
@Glenda.Lemke22, nextissue))))

thank you for reading) - Eunice_Prohaska8 commented on March 23rd 20 at 19:57

Find more questions by tags Vue.js