简体   繁体   English

在运行时node.js配置apiMiddleware路由

[英]configure apiMiddleware routes at runtime node.js

Is it possible to configure the apiMiddleware at runtime & route to respective class or api. 是否可以在运行时配置apiMiddleware并路由到相应的类或api。

E.g  class standard = 1st
     section  = A

http://myschool.com/school/1/A/attendance 

var apiMiddleware = {

    school : {

        get : {

            //class id = 1
            1 : {  
                A : {
                   attendance : myschool.attendance
                }            

            }
        },
        post : {

        }
    }

}

As per the above example, I am retrieve the attendance sheet of 1st std of sectionA. 按照上面的例子,我正在检索第一节a的出勤表。

If i what attendance sheet of 1st std sectionB I don't want to rewrite the same code as 如果我是第一节B的出勤表,我不想重写相同的代码

A : {
    attendance : myschool.attendance
  } 
B : {
    attendance : myschool.attendance
 } 

till ..... 12th std . 直到..... 12th std。

Out there, expert's could have come across this problem & found solution also. 在那里,专家可能会遇到这个问题并找到解决方案。 Could be great to hear the suggestion on the same. 很高兴听到关于同样的建议。

Use URL pattern with parameters: 使用带参数的URL模式:

http://myschool.com/:school/:class/:section/attendance

For example using expressjs : 例如使用expressjs

var express = require('express');
var app = express();

app.get('/:school/:class/:section', function(req,res,next){
   console.log(req.params);     
});

Here, the object req.params will have variable parts of the URL. 这里,对象req.params将具有URL的可变部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM