用于多条路由的Express Router:将路由存储在变量中的最佳方法

时间:2019-10-22 13:18:12

标签: express memory parameters router

我正在使用几个路由器文件在网站不同章节上的快速服务器上工作。最后,现有的哈巴狗页面不会有太大变化,但是在过渡期间,会有一些动态内容,主要是说“此页面尚未迁移(翻译),请参阅旧版本……”。

每章的页面数是给定的或非常缓慢地增加。因此,我列出了来自网站的允许请求以提供答案,此外还包含默认响应。

我做了几个变体,所有的变体都奏效,但是我问是否有一个或全部会引起内存问题,如果同时有很多需求。如果是这样,什么是好的做法?

  • 我对Node还是很陌生,无法发现模块上的变量是否是全局变量,因此会很快阻塞内存。 如果列表是分开的,以后添加新路由会更容易,但是如果需要,可以将它们集成到功能中。

我尝试使用字符串数组作为文件名,其中我仅具有文件名,还尝试使用对象数组,其中我具有文件名和临时内容。 路由器从请求中提取文件名,并将其与变量进行比较。如果存在,它将提供或创建该文件。

//Version array. (Past and future refer to the chronological order
// of the events allow to flip through the archive's years with an
//arrow on the pug page. They are defined by the index of the 
//filename in the array)

var targets=['2019-1','2018-1']; 
var current=targets[0];

router.get('/*',function(req,res) {
// get the name of the archive page from the link clicked 
    var event = (req.params[0]);

    if( targets.includes(event) ) {
        res.render('archive/'+event, {               
            title:'xyzzy - event in 20'+event, 
            message: 'page '+event+' coming soon ',
            event:event ,
            past: targets[targets.indexOf(event)+1],
            future: targets[targets.indexOf(event)-1] || current
            });
        } else {
            res.render('archive/'+current,
                {title:'xyzzy - Events', 
                message: 'no event for '+event+ '.'  });
        }
});

//Second version: several arrays of objects, one per language 

var etargets=[{c:'englishname',t:'englishtitle'}]

router.get('/*', function(req,res,next){

    const cible = (req.params[0]);
    var target = {t:'default title'}
    var route='default'

...

    if (etargets.find( ({c}) => c ===cible) ){

        target = etargets.find( ({c}) => c ===cible);
        route='e-asso/'+'cible';  
        ...
        }
...
    res.render(route,{title:target.t ||'rien'});
})

寻找有关存储列表的最佳做法和最佳版本的建议。路由器功能内的数组数组,对象对象...

0 个答案:

没有答案