【问题标题】:node.js/JavaScript syntax - add extra parameter to passed parametersnode.js/JavaScript 语法 - 向传递的参数添加额外的参数
【发布时间】:2019-05-14 12:46:36
【问题描述】:

我正在使用 node.js/Express 构建一个 REST API。

我将一些中间件应用于某些路由。我有一个无法解决的 JavaScript 语法错误。

server.js

const express = require('express')
const router = express.Router()


const watchdogController = {
  ping: function(req, res, next) {
    console.log('watchdog')
    res.status(200).send('woof!')
    //next()
  }
}
const middleware = function(req, res, next) {
  console.log('middleware')
  next()
}
const middleware2 = function(req, res, next, roles) {
  console.log('middleware2')
  //console.log(roles)   //I want to be able to view the roles here!
  next()
}


//This line is where I have the issue...
router.get('/watchdog', middleware, middleware2, watchdogController.ping)



module.exports = router

我需要能够将一组角色传递给middleware2。例如。

router.get('/watchdog', middleware, middleware2(...['ordinary','supervisor']), watchdogController.ping)

但是这个语法失败了:(

node server.js 结果:

middleware2
undefined
/Users/asdf7/Desktop/asdf7/lib/router.js:19
  next()
  ^

TypeError: next is not a function
    at middleware2 (/Users/asdf7/Desktop/eoh/lib/router.js:19:3)
    at Object.<anonymous> (/Users/asdf7/Desktop/eoh/lib/router.js:26:37)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/asdf7/Desktop/asdf7/index.js:2:16)

这行得通:

router.get('/watchdog', middleware, middleware2, watchdogController.ping)

但是现在我在 middleware2 中看不到任何角色;(我需要能够在 middleware2 函数中查看角色数组。

我不知道要使用什么语法...你们能帮忙吗?

【问题讨论】:

  • 只要get 的参数中的任何内容都是中间件,您就可以根据需要创建该中间件。最简单的方法是创建一个返回中间件的函数——然后将角色传递给中间件创建者函数。
  • express 中的中间件具有下一种格式(req、res、next)。如果您需要传递信息,则必须将此信息附加到先前中间件中的请求中,例如在中间件1上您可以添加 req.roles = 'WHATEVER';在下一次调用之后,您将能够访问中间件 2 上的 req.roles。
  • @DaveNewton 非常感谢您的回复。我不确定这将如何工作。创建中间件?到目前为止,我能想到的最好的方法是将middleware2 替换为:(req,res,next) =&gt; { middleware2(req,res,next,['ordinary','supervisor']) }。我以为我可以使用扩展运算符,但不知道如何...
  • @JoseMato 我有几十条路线(不仅仅是'/watchdog')。我试图在路由旁边传递角色以保持语法简洁。 middleware1 是 auth 的一个独立函数。我不希望它填充参数(并找出要填充的参数)。我只想在定义路由的同一行注入角色数组。我的目标是简洁的语法,而不是“让它工作”。
  • 没有。 createMiddleware2 = roles =&gt; { return function (req, res, next) { doSomethingWithRolesEtc... } }

标签: javascript node.js syntax


【解决方案1】:

解决方案(感谢@DaveNewton):

const express = require('express')
const router = express.Router()


const watchdogController = {
  ping: function(req, res, next) {
    console.log('watchdog')
    res.status(200).send('woof!')
    //next()
  }
}
const middleware = function(req, res, next) {
  console.log('middleware')
  next()
}
const middleware2 = roles => function(req, res, next) {
  console.log('middleware2')
  console.log(roles)
  next()
}

router.get('/watchdog', middleware, middleware2(['ordinary','supervisor']), watchdogController.ping)



module.exports = router

【讨论】:

  • 很高兴它成功了。请注意,可能还有其他和/或更好的解决方案,特别是因为我已经很长时间没有使用 Express 了——在他们的中间件文档中四处寻找并查看一些示例,看看是否有更清洁的方法。跨度>
猜你喜欢
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多