Hi, I’m Junning, the author of Deno from Start to Run and a Practical Guide to building HTTP Server based on Deno. I started with Deno just for the sake of getting paid for it, so I also challenged to get started with Deno based on zero one day. But Deno was so sweet that he spent a week learning about MongoDB in his spare time and wrote what he thought was a complete HTTP Server practice guide.

This article is an introduction to how I developed a Deno plug-in and published it to the official Deno plug-in library. The plugin is called Duck and is a small tool that automatically scans the Controller layer and registers routes. How to use this plug-in please click the logo direct:

The project structure

This project was born in order to automatically scan controllers while writing the practical Guide to Building HTTP Servers based on Deno. Referring to the automatic scanning controller of KOA by Liao Xuefeng, in addition to the different language, the implementation details and ideas are also different.

. / ├ ─ ─ LICENSE ├ ─ ─ the README. Md ├ ─ ─ addAbcControllers. Ts ├ ─ ─ addOakControllers. Ts ├ ─ ─ addServestControllers. Ts ├ ─ ─ mod. The ts └ ─ ─ the test ├ ─ ─ ABC │ ├ ─ ─ controllers │ │ └ ─ ─ the helloworld. Ts │ └ ─ ─ for server ts ├ ─ ─ oak │ ├ ─ ─ controllers │ │ └ ─ ─ the helloworld. Ts │ ├─ ├─ class.sci-impCopy the code
  • mod.ts: Deno official recommended plug-in entry file
  • test: Demo of some tests
  • add***Controllers.ts: compatible with common Http Server middleware plug-in, automatic judgment, no need for user parameters.

The implementation process

Automatic scanning controller

Note: here is the code snippet, click on the duck at the beginning to view the source code.

.constrealPath = Deno.realPathSync(dir); .for (const dirEntry of Deno.readDirSync(dir)) {
  if (dirEntry.name.endsWith(".ts") || dirEntry.name.endsWith(".js")) {
    const controller = await import(`file://${realPath}/${dirEntry.name}`);
    const method = controller.method || "get";
    const api = controller.api ||
    dirEntry.name.replace(/(.*\/)*([^.]+).*/ig."$2");
    router[method](` /${api}`, controller.default); }}Copy the code
  • According to the user passeddirGet the folder usedDeno.realPathSyncGet the real absolute path, which is used to get files.
  • useDeno.readDirSyncThe interface reads all files in the folderjstsThe document at the end will do.
  • useimportDynamic import module, there is a pit is not addedfile://The actual run will add the prefix automaticallyhttps://.
  • This assumes that if the controller does not export the name, it automatically reads the file name as the API name (here we borrow from umijs).
  • Finally, mount the controller dynamically to the Router (which is also passed in by the user).

Intelligent compatible

This is really nothing to say, is the manual comparison of these plug-ins with unique attributes to identify which plug-ins, currently support servest, Oak, ABC, these three high-profile projects.

export default async function (router: any, dir: string = "controllers") {
  if (router.handle) {
    return  await addServestControllers(router,dir);
  } else if(router.middleware) {
    return  await addAbcControllers(router,dir);
  } else {
    return awaitaddOakControllers(router,dir); }}Copy the code

Out of the friendly

With DUCK, there are two routing paths given by default, and checkHealth is a required interface for the project basically.

. router.get("/".(ctx: any) = > {
  ctx.response.body = "Hello Oak!";
}).get("/checkHealth".(ctx: any) = > {
  ctx.response.body = "The server is health. Just do it."; }); .Copy the code

Release the plugin

1. Open deno.land/x

2. Click Add a Module

3. Create a public repository hosted on GitHub as shown in the figure below

4. Choose a good name and register as soon as possible, otherwise you will be robbed by JustJavac. Maybe you are afraid of me.

5, the following is a prompt to add GitHub webhook, and then the release is successful, I just tried superman has not been registered.

Last doghead building!!

This article was first published in Yang Junning’s blog, it is not easy to create, your praise 👍 is my motivation!!