Nodejs ejs view engine, passing parameters to different routes right away?

I have 4 different routes in my routes> intro.js file and you want to pass the same variable for each route.

(I am using nodejs express with ejs view engine)

// intro.js file

var something = {
    common: 'common',
    different: {
        test1: 'test1',
        test2: 'test2',
        test3: 'test3',
        test4: 'test4'
    }
}

router.get('/test1', (req, res) => {
    res.render('test1', {common: something.common, different: something.different.test1});
}

router.get('/test2', (req, res) => {
    res.render('test2', {common: something.common, different: something.different.test2});
}

router.get('/test3', (req, res) => {
    res.render('test3', {common: something.common, different: something.different.test3});
}

router.get('/test4', (req, res) => {
    res.render('test4', {common: something.common, different: something.different.test4});
}

There seems to be a better way to pass a common (something.common) variable for each route (ejs view files) as something.common repeats.

In fact, the view file that receives this shared variable is a regular view file. This view file is included in the file test1, test2, test3, test4 separately, using

<% include 'view_file' %>

Is there a way to pass this repeating variable to all routes at once?

Having some difficulties, any advice can really be appreciated.

+4
source
1

, app.locals

4 app.locals .

app.locals = {
  something: {
    common: 'common'
}

:

.

: , , Express/ Node.JS?

+2

Source: https://habr.com/ru/post/1694342/


All Articles