Node.js express4:路由器语法

Node.js express4:路由器语法,node.js,rest,express,express-4,Node.js,Rest,Express,Express 4,我正在使用带有新路由器的Express 4。至少有一件事继续困扰着我,那就是语法问题——我想知道是否有一个正则表达式可以满足我的需要。我有一个标准的RESTAPI,但我想添加批量更新,这样我就可以发送所有信息,用一个请求更新一些用户模型,而不是每个用户一个PUT请求,例如。无论如何,我当前将所有请求路由到用户资源,如下所示: app.use('/users, userRoutes); 在userRoutes.js中: router.get('/', function (req, res, ne

我正在使用带有新路由器的Express 4。至少有一件事继续困扰着我,那就是语法问题——我想知道是否有一个正则表达式可以满足我的需要。我有一个标准的RESTAPI,但我想添加批量更新,这样我就可以发送所有信息,用一个请求更新一些用户模型,而不是每个用户一个PUT请求,例如。无论如何,我当前将所有请求路由到用户资源,如下所示:

app.use('/users, userRoutes);
在userRoutes.js中:

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

   //gets all users
});


router.put('/:user_id', function (req, res, next) {

   //updates a single user
});
但现在我想要一个捕获批处理请求的路由,类似这样:

router.put('/Batch', function (req, res, next) {


     //this picks up an array of users from the JSON in req.body and updates all

});
var express = require('express');
var router = express.Router();

router.get('/',            function (req, res) {}); // gets all users
router.post('/:user_id',   function (req, res) {}); // one user
router.put('/:user_id',    function (req, res) {}); // one user
router.patch('/:user_id',  function (req, res) {}); // one user
router.delete('/:user_id', function (req, res) {}); // one user

app.use('/user', router);  // Notice the /user/:user_id is *singular*

var pluralRouter = express.Router();
pluralRouter.post('/', function (req, res) {
   // req.body is an array.  Process this array with a foreach
   // using more or less the same code you used in router.post()
});
pluralRouter.put('/', function (req, res) {
   // req.body is another array.  Have items in the array include
   // their unique ID's.  Process this array with a foreach
   // using +/- the same code in router.put()
});

app.use('/users', pluralRouter);  // Notice the PUT /users/ is *plural*
换句话说,我想要的东西可以翻译成:

app.use('/usersBatch, function(req,res,next){

  }
…但是有了新的路由器。我不能理解正确的语法

我试过这个:

app.use('/users*, userRoutes);

但这不起作用。有人知道如何设计它吗?

REST没有将POST作为列表语法。这是因为REST中的每个URL都指向一个单独的资源

作为一名互联网工程师,我没有看到过任何批量发布的文章,但也就是说,这是你的应用程序,所以你可以制作任何你喜欢的API。肯定有它的用例

你仍然需要描述它来表达。我会这样做:

router.put('/Batch', function (req, res, next) {


     //this picks up an array of users from the JSON in req.body and updates all

});
var express = require('express');
var router = express.Router();

router.get('/',            function (req, res) {}); // gets all users
router.post('/:user_id',   function (req, res) {}); // one user
router.put('/:user_id',    function (req, res) {}); // one user
router.patch('/:user_id',  function (req, res) {}); // one user
router.delete('/:user_id', function (req, res) {}); // one user

app.use('/user', router);  // Notice the /user/:user_id is *singular*

var pluralRouter = express.Router();
pluralRouter.post('/', function (req, res) {
   // req.body is an array.  Process this array with a foreach
   // using more or less the same code you used in router.post()
});
pluralRouter.put('/', function (req, res) {
   // req.body is another array.  Have items in the array include
   // their unique ID's.  Process this array with a foreach
   // using +/- the same code in router.put()
});

app.use('/users', pluralRouter);  // Notice the PUT /users/ is *plural*
还有其他方法可以做到这一点。包括在URL中放置逗号分隔的参数。例如

GET /user/1,2,3,4
但这并不是很好用,在PUT或POST中也很模糊。并行阵列是邪恶的


总之,这是一个利基用例,所以做最有效的。记住,服务器是为客户机服务的。了解客户的需求和服务。

我猜对
[PUT]/users/Batch
的调用正在被
[PUT]/users/:user\u id
路由接收。字符串
/:user_id
用作正则表达式,使其也收集
/Batch

您可以在路由顺序中将
/Batch
移动到
/:user\u id
之前,优化
/:user\u id
的正则表达式以不捕获
/Batch
或将
/Batch
更改为不会过早获取的内容


(加上迈克尔所说的一切)

谢谢乔,事实上我确信情况并非如此。我从客户端请求的实际URL是/usersBatch(没有第二个斜杠)。新的Express 4路由器是werid,因为它要求您从路由器中剥离“xxx”。很难解释,所以我不想麻烦了。这篇文章演示了我想说的:…基本上,它是app.use('/api',myRouter);然后myRouter.get('/users')实际上指向了一个原始的'/api/users'.Ah,我假设您使用的是分割路由(第二个斜杠)。希望你能弄明白。我想我还没有弄明白(不记得了),但当我弄明白后,我会把它发回这个线程。我想批量处理的一个原因是,我不喜欢在我的服务器和浏览器开发控制台中看到所有的请求!所以,基本上你们要说的是,我需要一个全新的路由器实例来执行/usersBatch,而不是将该路由和/users的其他路由混为一谈。看起来很不幸。一定有一些正则表达式我可以放在应用程序中。使用('/users….',routeHandler)可以做到这一点!顺便说一句,我考虑批处理的方式是在客户端上使用JSON-->var batch={create:[],update:[],destroy:[]}。然后在对服务器的一个请求中,我可以指定要更新的模型、要创建的模型、要删除的模型等等。显然,对于从不同位置收到大量请求的服务器,这将比较小的、更多的请求占用服务器稍长一点,但我认为这可能值得一试。很惊讶你没有看到太多。嘿,Olegzandr,
路由器
是免费的,所以不用担心:-)每个API都有一个路由器是很常见的。如果您不喜欢在控制台中登录,可以将其关闭-尝试删除记录器。也许是“摩根”?最后,无长请求比短请求更能占用服务器。扩展简单的小东西要容易得多。