关于后端:关于-Express-API-appuse-中的-path-参数用法

3次阅读

共计 1157 个字符,预计需要花费 3 分钟才能阅读完成。

SAP UI5 Tools 的 ui5.yaml 文件:

specVersion: "2.6"
type: application
metadata:
  name: my.application
server:
  customMiddleware:
    - name: myCustomMiddleware
      mountPath: /myapp
      afterMiddleware: compression
      configuration:
        debug: true

其中 mouthPath 的值会传递给 app.use 的第一个 path 参数。

path 能够指定一个具体的门路,比方下列代码,匹配门路 abcd

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

也能够应用正则表达式。下列门路,匹配 abcdabd

app.use('/abc?d', function (req, res, next) {next()
})

下列门路,匹配 abcdabbcdabbbcd 等等:

app.use('/ab+cd', function (req, res, next) {next()
})

下列门路匹配 adabcd

app.use('/a(bc)?d', function (req, res, next) {next()
})

下列门路匹配 abcxyz

app.use(/\/abc|\/xyz/, function (req, res, next) {next()
})

路由将匹配紧随其门路的任何门路,并带有“/”。例如:app.use(‘/apple’, …) 将匹配“/apple”、“/apple/images”、“/apple/images/news”等。

因为门路默认为“/”,因而对于应用程序的每个申请,都会执行没有门路装置的中间件。
例如,这个中间件函数将为应用程序的每个申请执行:

app.use(function (req, res, next) {console.log('Time: %d', Date.now())
  next()})

上面是执行的成果:

如果删除了 next 调用,其余的中间件将永远没有机会失去执行:

错误处理中间件总是须要四个参数。必须提供四个参数以将其标识为错误处理中间件函数。

即便您不须要应用下一个对象,您也必须指定它来保护签名。否则,下一个对象将被解释为惯例中间件,并且无奈处理错误。无关错误处理中间件的详细信息,

以与其余中间件函数雷同的形式定义错误处理中间件函数,惟一区别就是应用 四个 参数而不是三个参数,特地是应用签名 (err, req, res, next)):

app.use(function (err, req, res, next) {console.error(err.stack)
  res.status(500).send('Something broke!')
})
  1. List item
正文完
 0