当前位置: 首页 > Web前端 > HTML5

SAPUI5工具如何开发自定义中间件扩展-CustomMiddlewareExtension

时间:2023-04-04 23:12:25 HTML5

CustomMiddlewareExtension(自定义中间件扩展)由ui5.yaml和自定义中间件实现组成。它可以是一个独立的模块,也可以是现有UI5项目的一部分。这是一个ui5.yaml示例:specVersion:"2.6"kind:extensiontype:server-middlewaremetadata:name:markdownHandlermiddleware:path:lib/middleware/markdownHandler.js自定义中间件扩展可以是作为依赖项处理的独立模块,通过在中定义依赖项来导入包.json。或者,可以在UI5项目中实现自定义中间件扩展。在这种情况下,扩展的配置是ui5.yaml中项目配置的一部分,如下所示。UI5Server将检测项目的自定义中间件配置并在启动时使用中间件。查看具体的例子:specVersion:'2.6'kind:projecttype:applicationmetadata:name:"sap.m.tutorial.walkthrough.140"server:customMiddleware:-name:markdownHandlerbeforeMiddleware:serveResourcesresources:configuration:paths:webapp:.---#CustommiddlewareextensionaspartofyourprojectspecVersion:"2.6"kind:extensiontype:server-middlewaremetadata:name:markdownHandlermiddleware:path:lib/markdownHandler.js上面的例子启动后,遇到如下错误消息:ErrorMessage:middlewareRepository:无法为markdownHandler要求中间件模块:找不到模块'C:\Code\UI5\Walkthrough\140\lib\middleware\markdownHandler.js'需要堆栈:C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\middlewareRepository.jsC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.jsC:\代码\UI5\演练\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\server.jsC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\index.jsC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\lib\cli\commands\serve.jsC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\yargs\index.cjsC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\bin\ui5.jsC:\app\node-v14.15.0-win-x64\node_modules\@ui5\cli\node_modules\import-local\index.jsC:\app\node-v14.15.0-win-x64\node_modules\@ui5\cli\bin\ui5.js在lib文件夹下创建markdownHandler.js文件后:旧错误消失,遇到新错误:TypeError:middlewareisnotafunctionatC:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:253:14atMiddlewareManager.addMiddleware(C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:89:38)在MiddlewareManager.addCustomMiddleware(C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\中间件\中间件eManager.js:237:15)在MiddlewareManager.applyMiddleware(C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\middleware\MiddlewareManager.js:38:14)在异步Object.serve(C:\Code\UI5\Walkthrough\140\node_modules\@ui5\cli\node_modules\@ui5\server\lib\server.js:166:3)在异步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}){返回函数(req,res,next){//[...]console.log('jerry');}};终于可以在ui5serve执行的命令行窗口中看到自定义中间件扩展打印出来了jerry:但是在localhost:8080之后遇到了如下错误:ERR_NETWORK_IO_SUSPENDED解决方法在自定义中间件实现中调用next函数即可:完整的源代码://自定义中间件实现module.exports=function({resources,middlewareUtil,options}){constMarkdownIt=require('markdown-it');constmd=newMarkdownIt();返回函数(req,res,next){if(!req.path.endsWith(".html")){//不处理非HTML请求next();return;}//尝试读取相应的markdown文件resources.rootProject.byPath(req.path.replace(".html",".md")).then(async(resource)=>{if(!resource){//没有找到文件,交给下一个中间件next();return;}constmarkdown=awaitresource.getBuffer();//生成来自markdown字符串的HTMLconsthtml=md.render(markdown.toString());res.type('.html');res.end(html);}).catch((err)=>{next(err);});}};