directus 的endpoint功能
官方文档 (endpoints) 还是写的不够全,踩过了很多坑;
本文使用的node版本为v22.12.0; directus@11.11.0
前沿:
首先遇到的坑是,之前用低版本的node,在安装directus时有warn,但是功能是正常的。就是为了验证endpoint 时,功能验证不成功(其实是写法的问题GPT坑了我)怀疑是node版本低了。
所以升级到v23,然后再安装directus(npm install directus@11.11.0)时就彻底安装不成功了。
经过跟GPT的斗智斗勇,折腾了很久(给了我一通的解决方案),结果有个包isolated-vm需要FQ下载。
回归正题:
在项目的 ./extensions(需要跟.env中的EXTENSIONS_PATH配置保持一致 ) 文件夹下面执行指令:
npx create-directus-extension@latest
安装提示选择endpoint
Choose the extension type endpoint
✔ Choose a name for the extension endpoint-checkout
✔ Choose the language to use javascript
✔ Auto install dependencies? Yes
✔ Done
Your endpoint extension has been created at /Users/dingyuwang/3-Code/demo/muxige/extensions/endpoint-checkout
To start developing, run:
cd endpoint-checkout
npm run dev
To build for production, run:
npm run build
在extensions/endpoint-checkout 中必须要执行 npm run build;
否则,npx directus start执行时会报错。
正常的信息应该是:
npx directus start
[15:52:56.805] INFO: Extensions loaded
[15:52:56.809] INFO: Loaded extensions: endpoint-checkout, test
[15:52:56.824] WARN: "PUBLIC_URL" should be a full URL
[15:52:56.908] INFO: Server started at http://0.0.0.0:8055
可执行:curl http://localhost:8055/endpoint-checkout
返回:Hello, World!
/*
*访问路径分别是:
* http://localhost:8055/greet
* http://localhost:8055/greet/intro
* http://localhost:8055/greet/goodbye
* 千万不要画蛇添足,在url中加文件夹的名字
* id就是访问的根路径,id+router路径
*/
export default {
id: "greet",
handler: (router, countext) => {
router.get("/", (req, res) => res.send("Hello, World!"));
router.get("/intro", (req, res) => res.send("Nice to meet you."));
router.get("/goodbye", (req, res) => res.send("Goodbye!"));
},
};
愿意一起学习的小伙伴,可以加qq: 285861181 ,共同交流。
本文来自博客园,作者:egreen,转载请注明原文链接:https://www.cnblogs.com/egreen/p/19076929

浙公网安备 33010602011771号