使用Express.js(node.js)进行动态路由的最佳方式 [英] Best way to do dynamic routing with Express.js (node.js)

查看:1171
本文介绍了使用Express.js(node.js)进行动态路由的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的CMS与express.js动态创建路由。
从数据库中获取一个类似于下面的JSON:

I'm trying to create a simple CMS with express.js that dynamically creates routes. It gets a JSON from a database that looks like this:

pagesfromdb = {
    home = {
        paths = ['/','/home','/koti'],
        render = 'home.ejs',
        fi_FI = {html='<h1>Hei maailma!</h1>'},
        en_US = {html='<h1>Hello World!</h1>'}
    },
    about = {
        paths = ['/about','/tietoja'],
        render = 'general.ejs',
        fi_FI = {html='Tietoja'},
        en_US = {html='About Us'}
    }
}

像这样:

Object.keys(pagesfromdb).forEach(function(key) {
    var page = pagesfromdb[key];
    app.get(page.global.paths,function(req, res){
         res.render(page.render, page[language]);
    });
});

现在一切正常。但问题是,每次用户修改内容和路径时,整个节点应用程序需要重新启动。
我没有找到任何API调用来删除路由。

Now everything is working fine. But the problem is, every time a user modifies the content and paths, the whole node app needs to be restarted. I didin't find any API calls to remove routes.

有没有办法安全地删除使用app.get设置的旧路由?我是否应该这样做?

Is there any way to safely remove the old routes set with app.get? Should I even do that?

有更好的方法来做这种路由吗?我喜欢这个方法,因为它允许我使用内置函数,是快速和支持regex。

Is there a better way to do this kind of routing? I do like this method as it allows me to use the built in function, is fast and supports regex.

我尝试删除整个app.routes与app.routes =

I tried removing the whole app.routes with app.routes = nul but it didn't do anything, the old routes were still in place.

确实删除了它们的一件事是

One thing that did indeed remove them was

delete app._router.map.get;
app._router.map.get = [];

但这确实删除了它们,是安全使用,所以我不会最终劫持巨大

But does this actually remove them and is it safe to use so I don't end up hijacking huge amounts of ram if the router keeps getting repopulated?

推荐答案

正如@supermova在评论中所说,可以更新Express on飞行。另一个需要考虑的架构是类似于经典CMS(例如Wordpress)的架构。在其他CMS中,所有请求都进入相同的回调并在每个请求中查找,您会在数据库中查找为该网址提供的网页。

As @supermova said in the comments, it is possible to update Express on-the-fly. Another architecture to consider is the one similar to classic CMSs (like Wordpress, for example). In other CMSs, all requests go to the same 'callback' and on every request you look up in the database what page to serve for that URL.

app.get('/*', function (req, res) {
   db.findPage({ slug: req.url}, function (err, pageData) {
       res.render('page-template', {
           pageContent: pageData.content,
           pageTitle: pageData.title
       });
   });
});

这种方法的结果是速度显着降低,但最终我认为更健全。如果速度是一个巨大的问题,你可以设置一个缓存系统(如Varnish),但将有头痛的修改快速路线的方法在飞。例如,如果你必须扩展到两个Web服务器呢?如果服务器A获得创建页面请求,因此它知道更新其路由,但是服务器B怎么办?每次请求到数据库,你都可以更好地横向扩展。

There is a significant speed decrease as a result of this method, but in the end I think it is more sane. If speed is a huge issue you can set up a caching system (like Varnish) but there will be headaches with the approach of modifying Express routes on-the-fly. For example, what if you have to scale to two web servers? How do you keep them in sync if server A gets the 'create page' request and so it knows to update its routes, but what about server B? With every request going to the database you will be able to scale horizontally better.

这篇关于使用Express.js(node.js)进行动态路由的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆