打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何开发一个 SAP UI5 Tools 的自定义中间件扩展 - Custom Middleware Extension
userphoto

2022.10.08 四川

关注

自定义中间件扩展(Custom Middleware Extension)由 ui5.yaml 和自定义中间件实现组成。 它可以是一个独立的模块,也可以是现有 UI5 项目的一部分。

下面是一个 ui5.yaml 的例子:

specVersion: "2.6"
kind: extension
type: server-middleware
metadata:
  name: markdownHandler
middleware:
  path: lib/middleware/markdownHandler.js

自定义中间件扩展可以是作为依赖项处理的独立模块,通过在 package.json 里定义依赖的方式引入。

或者,可以在 UI5 项目中实现自定义中间件扩展。 在这种情况下,扩展的配置是 ui5.yaml 中项目配置的一部分,如下所示。

UI5 Server 将检测项目的自定义中间件配置,并在启动时使用中间件。

看个具体的例子:

specVersion: '2.6'
kind: project
type: application
metadata:
  name: "sap.m.tutorial.walkthrough.140"
server:
  customMiddleware:
    - name: markdownHandler
      beforeMiddleware: serveResources
resources:
  configuration:
    paths:
      webapp: .
---
# Custom middleware extension as part of your project
specVersion: "2.6"
kind: extension
type: server-middleware
metadata:
  name: markdownHandler
middleware:
  path: lib/markdownHandler.js

上面的例子启动后,遇到如下错误消息:

Error Message: middlewareRepository: Failed to require middleware module for markdownHandler: Cannot find module 'C:\Code\UI5\Walkthrough\140\lib\middleware\markdownHandler.js' Require stack: - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\middlewareRepository.js - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\server.js - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\index.js - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\lib\cli\commands\serve.js - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\yargs\index.cjs - C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\bin\ui5.js - C:\app\node-v14.15.0-win-x64\node_modules\@ui5\cli\node_modules\import-local\index.js - C:\app\node-v14.15.0-win-x64\node_modules\@ui5\cli\bin\ui5.js

在 lib 文件夹下新建一个 markdownHandler.js 文件之后:

旧的错误消失,又遇到了新的错误:

TypeError: middleware is not a function at C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:253:14 at MiddlewareManager.addMiddleware (C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:89:38) at MiddlewareManager.addCustomMiddleware (C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:237:15) at MiddlewareManager.applyMiddleware (C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:38:14) at async Object.serve (C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\server.js:166:3) at async Object.serve.handler (C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\lib\cli\commands\serve.js:130:33)

添加如下的实现之后:

module.exports = function({resources, middlewareUtil, options}) {
    return function (req, res, next) {
        // [...]        console.log('jerry');
    }};

终于可以在 ui5 serve 执行的命令行窗口,看到自定义中间件扩展打印出的 jerry 了:

但是 localhost:8080 之后遇到如下错误:

ERR_NETWORK_IO_SUSPENDED

解决方案

在自定义中间件实现里,调用 next 函数即可:

完整的源代码:

// Custom middleware implementationmodule.exports = function({resources, middlewareUtil, options}) {
    const MarkdownIt = require('markdown-it');
    const md = new MarkdownIt();
    return function (req, res, next) {
        if (!req.path.endsWith(".html")) {
            // Do not handle non-HTML requests            next();
            return;
        }
        // Try to read a corresponding markdown file        resources.rootProject.byPath(req.path.replace(".html", ".md")).then(async (resource) => {
            if (!resource) {
                // No file found, hand over to next middleware                next();
                return;
            }
            const markdown = await resource.getBuffer();
            // Generate HTML from markdown string            const html = md.render(markdown.toString());
            res.type('.html');
            res.end(html);
        }).catch((err) => {
            next(err);
        });
    }};
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
node.js 报错 throw new TypeError(‘app.use() requires a middleware function‘)
Vue 3.0 使用 Vuetify中的坑 | Vue.js 技术论坛
Vue Electron开发跨平台桌面应用
第17章 前端UI框架
vue组件库开发
Cordova 3.x 基础(10)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服