jnrcrown.blogg.se

Pass data between routes express node to pug template
Pass data between routes express node to pug template







pass data between routes express node to pug template

Then your function should be export the data. In case it is of any use, you can also send and receive data in the same request if you prefer, as in your comment this seems to be what you are trying to do. If there is some processing to be done that exceeds the ability of your template engine, you can always build your own module to do the processing and return the processed data to your template engine for rendering.You can use app.post('/getData', function(req, res)) If you want your app to be stateless, you can instead pass your to somewhere external, like a remote server like mysql which is independant of the express server. Specifically, when is set to 'production', and Pug is used with Express, the compileDebug option is false by default, while the cache option is true. I don't really have any experience with it, so I can't really say for sure, but you might give it a look.Īs far as using template engines to build your pages goes, I think that as long as you are gathering the data on the server side, it is pretty acceptable to use your template engine to insert the data into your pages. Express and Pug automatically modify the defaults of a few options in production environment, to provide a better out-of-the-box experience for users. There might also be a way to do this with socket.io that would keep you from having to make another route. The block keyword allows us to extend a template through inheritance. This file acts as a sort of boilerplate for our other templates. First, create a default.pug file in the views directory and paste in the following content.

pass data between routes express node to pug template pass data between routes express node to pug template

It sounds like, given your limitations, this might be your only (or at least most reasonable) solution. We can demonstrate the power of Pug and Express by building a simple website. The main things I would suggest there is that you make sure you aren't in violation of the API provider's terms of use, and that you aren't just creating an open proxy to their API. I was thinking the same thing about creating another route to specifically get the data with AJAX.









Pass data between routes express node to pug template