Javascript 使用express和node加载路径的JSON配置

Javascript 使用express和node加载路径的JSON配置,javascript,json,node.js,express,Javascript,Json,Node.js,Express,所以我有一个关于express和node.js中路由的可能是noob的问题 我有一个中间件,它加载一个JSON文件,其中包含所有路径和这些路径的配置。配置文件如下所示 module.exports = { "/account" :{ template: "dashboard/account", page_title: "My Account", // more path configs.... }, "/account/:id/users" : { templat

所以我有一个关于express和node.js中路由的可能是noob的问题

我有一个中间件,它加载一个
JSON文件
,其中包含所有路径和这些路径的配置。配置文件如下所示

module.exports = {
"/account" :{
    template: "dashboard/account",
    page_title: "My Account",
    // more path configs....
},
"/account/:id/users" : {
    template: "dashboard/account/account-users",
    page_title: "Account Users",
    // more path configs....
}
}
var routeConfig = require('../config/route-config'); // path to the JSON above
getRouteConfig: function (req, res, next) {
    req.config = routeConfig[req.path];
    next();
},
app.get('/account/:id/users',  function(req, res){
  // Get the users
  // 
});
以及创建应用程序时在我的应用程序服务器中调用的中间件方法

app.use(middleware.getRouteConfig);
看起来像这样

module.exports = {
"/account" :{
    template: "dashboard/account",
    page_title: "My Account",
    // more path configs....
},
"/account/:id/users" : {
    template: "dashboard/account/account-users",
    page_title: "Account Users",
    // more path configs....
}
}
var routeConfig = require('../config/route-config'); // path to the JSON above
getRouteConfig: function (req, res, next) {
    req.config = routeConfig[req.path];
    next();
},
app.get('/account/:id/users',  function(req, res){
  // Get the users
  // 
});
最后我的快车路线是这样的

module.exports = {
"/account" :{
    template: "dashboard/account",
    page_title: "My Account",
    // more path configs....
},
"/account/:id/users" : {
    template: "dashboard/account/account-users",
    page_title: "Account Users",
    // more path configs....
}
}
var routeConfig = require('../config/route-config'); // path to the JSON above
getRouteConfig: function (req, res, next) {
    req.config = routeConfig[req.path];
    next();
},
app.get('/account/:id/users',  function(req, res){
  // Get the users
  // 
});
现在让我们假设我转到/account/12345/users

Express注意到路线有“代码>:ID 中间,并走到那条路线。但是,我的中间件随后将尝试加载该路由的

配置
,但在我的JSON配置文件中找不到/account/12345/users


因此,我的问题是如何使用我的dynamic:id path变量加载路由配置,并从JSON文件获取配置。提前感谢

使用Google的GSON SDK实现json解析器

您将从以下位置找到示例代码:

对于json字符串验证程序: