相同的路由,不同的查询但不同的中间件

huangapple go评论60阅读模式
英文:

Same route, different query but different middlewares

问题

I understand your request. Here's the translated code portion without the parts you mentioned not to translate:

我正在处理一个具有以下路由的 API(全部都是 POST 请求)

`/friends?d=onlineFriends`
`/friends?d=offlineFriends`
等等…

以及它是如何处理的:

`server.js`
```js
app.post("/friends", (req, res, next) => {
  let d = req.query.d;
  let path = "./funcs/" + d + ".js";
  return require(path)(req, res, next);
})

./funcs/onlineFriends.js

module.exports = (req, res, next) => {
  return res.sendStatus(200);
}

但问题是,我想为每个函数使用不同的中间件。使用上面的代码,如果我想使用中间件,它将应用于所有函数,因为你必须将它放在 app.post 部分。

我尝试过以下方法:

module.exports = (req, res, next) => {
  middleware(req, res, next);
  return res.sendStatus(200);
}

但当然会导致 Cannot set headers after they are sent to the client 错误。

我知道你可能会问:“为什么不使用像 /friends/online 这样的路由?”但我真的无法更改客户端,我必须这样做。


Please note that I've translated the code portion while excluding the parts you specified not to translate.

<details>
<summary>英文:</summary>

I&#39;m working on an API that has routes like this (all POST requests)

`/friends?d=onlineFriends`
`/friends?d=offlineFriends`
and on...

and this is how it&#39;s handled:

`server.js`
```js
app.post(&quot;/friends&quot;, (req, res, next) =&gt; {
  let d = req.query.d
  let path &quot;./funcs/&quot; + d + &quot;.js&quot;
  return require(path)(req, res, next)
})

./funcs/onlineFriends.js

module.exports = (req, res, next) =&gt; {
  return res.sendStatus(200)
}

But the thing is, I want to use different middlewares per func, with the code above if I wanted to use a middleware it would apply to all funcs because you'd have to put it in app.post part.

I've tried following:

module.exports = (req, res, next) =&gt; {
  middleware(req, res, next)
  return res.sendStatus(200)
}

but of course it results in Cannot set headers after they are sent to the client.

I know you might ask "Why not use a router like /friends/online", I really can't change the client and this is how I must do it.

答案1

得分: 1

如果您有中间件 abc,您可以动态选择它们的组合并使用它们来处理请求:

app.post("/friends", function(req, res, next) {
  var middleware;
  switch (req.query.d) {
    case "a": middleware = [a, c]; break;
    case "b": middleware = [b]; break;
    default: return next();
  }
  express.Router().use(middleware)(req, res, next);
});
英文:

If you have middlewares a, b and c, you can dynamically choose a combination of them and use that to handle the request:

app.post(&quot;/friends&quot;, function(req, res, next) {
  var middleware;
  switch (req.query.d) {
    case &quot;a&quot;: middleware = [a, c]; break;
    case &quot;b&quot;: middleware = [b]; break;
    default: return next();
  }
  express.Router().use(middleware)(req, res, next);
});

huangapple
  • 本文由 发表于 2023年1月4日 19:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75004853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定