对于支持params的express 4.x route(';/path';)是否有解决方案?

对于支持params的express 4.x route(';/path';)是否有解决方案?,express,routing,Express,Routing,我正在使用ExpressJS4.x在mongodb之上构建一个简单的api api需要提供几组数据: /api/v1/datatype1 /api/v1/datatype2 对于每种数据类型,我都有CRUD操作(post、get、put、delete) api请求如下所示: POST /api/v1/datatype1 GET /api/v1/datatype1:_id PUT /api/v1/datatype1:_id DELETE /api/v1/datatype1:_id dataT

我正在使用ExpressJS4.x在mongodb之上构建一个简单的api

api需要提供几组数据:

/api/v1/datatype1 /api/v1/datatype2

对于每种数据类型,我都有CRUD操作(post、get、put、delete)

api请求如下所示:

POST /api/v1/datatype1
GET /api/v1/datatype1:_id 
PUT /api/v1/datatype1:_id 
DELETE /api/v1/datatype1:_id
dataType1ApiRouter.param("entity_id", function (req, res, next, id) {
   //async db fetch here by id, then call next with fetched data 
   //or error if faild request/not found entity.
   //let's say req.dataEntity = dataEtity; next();
} );
 dataType1ApiRouter.route("/datatype1")
      .get(":entity_id", function (req, res, next) {
           //expcet req.dataEntity to be fetched by the param filter.
      })
      .post(function(req, res, next) {
          //just create an new dataType1 entity.
      });
如果我创建如下路由器参数:

POST /api/v1/datatype1
GET /api/v1/datatype1:_id 
PUT /api/v1/datatype1:_id 
DELETE /api/v1/datatype1:_id
dataType1ApiRouter.param("entity_id", function (req, res, next, id) {
   //async db fetch here by id, then call next with fetched data 
   //or error if faild request/not found entity.
   //let's say req.dataEntity = dataEtity; next();
} );
 dataType1ApiRouter.route("/datatype1")
      .get(":entity_id", function (req, res, next) {
           //expcet req.dataEntity to be fetched by the param filter.
      })
      .post(function(req, res, next) {
          //just create an new dataType1 entity.
      });
如果我创建这样的路线:

POST /api/v1/datatype1
GET /api/v1/datatype1:_id 
PUT /api/v1/datatype1:_id 
DELETE /api/v1/datatype1:_id
dataType1ApiRouter.param("entity_id", function (req, res, next, id) {
   //async db fetch here by id, then call next with fetched data 
   //or error if faild request/not found entity.
   //let's say req.dataEntity = dataEtity; next();
} );
 dataType1ApiRouter.route("/datatype1")
      .get(":entity_id", function (req, res, next) {
           //expcet req.dataEntity to be fetched by the param filter.
      })
      .post(function(req, res, next) {
          //just create an new dataType1 entity.
      });
我得到一个语法错误。route.get和.post(以及类似的其他方法)只需要一个参数,从而导致错误:

  Route.get() requires callback functions but got a [object String]

是否有一种方法可以将所有“/datatype1”请求实际分组到一个url声明下,而不是对每个需要post方法所需id的方法重复该方法(“datatype1:entity_id”),但是,您可以考虑用另一个<代码>路由器<代码>来代替这个代码>路由> <代码>。然后,你就可以挂载那个子路由器了

基本示例,修改您提供的代码:

var mainRouter = express.Router(),
    subrouter = express.Router();

subrouter.param("entity_id", function (req, res, next, id) {
    // param handler attached to subrouter
});


subrouter.post('/', function(req, res, next) {
    // post handler attached to base mount-point
});

subrouter.get("/:entity_id", function (req, res, next) {
    // get handler attached to base mount-point/<id>
});

// here we mount the sub-router at /datatype1 on the other router
mainRouter.use('/datatype1', subrouter);
var mainRouter=express.Router(),
子程序=express.Router();
子程序参数(“实体id”,函数(req、res、next、id){
//连接到子计算机的参数处理程序
});
子程序post(“/”,函数(req、res、next){
//连接到基本安装点的post处理程序
});
子程序get(“/:entity_id”,函数(req、res、next){
//将处理程序连接到基本装载点/
});
//在这里,我们将子路由器安装在另一个路由器上的/datatype1
mainRouter.use('/datatype1',子计算机);

请注意,这需要在URL中添加一个“/”,因此不是/api/v1/datatype1[someidhere],而是/api/v1/datatype1/someidhere

谢谢您的回答。你能给我一个简短的例子说明一个子程序是什么样子吗?给你增加了一个例子。这是一个非常有趣的想法。我不知道你能做到!不知道我是否可以说mainRouter.use(“/datatype1/:entity\u id”,subrouter),那会很震撼!