DEV Community

Cover image for Opine Tutorial Part 1: Express for Deno

Opine Tutorial Part 1: Express for Deno

Craig Morten on May 26, 2020

In this first Opine article we answer the question "What is Opine?". We'll outline the main features, and show you some of the main building block...
Collapse
 
jdelarubia profile image
jdelarubia

Hi Craig,
At the beginning, the line
import opine from "https://deno.land/x/opine@master/mod.ts";
should read:
import opine from "https://deno.land/x/opine@main/mod.ts"; ?
Just changing @master for @main.

Collapse
 
craigmorten profile image
Craig Morten • Edited

Yes you are correct - thanks for pointing it out, Will update 😄

Update: all changed - thanks!

Collapse
 
iampeters profile image
Peters Chikezie • Edited

this is awesome. looks very easy to use. thanks

Collapse
 
iampeters profile image
Peters Chikezie

I tried to use your example to get it to work with my code but I got this error

NotFound: No such file or directory (os error 2)
at unwrapResponse ($deno$/ops/dispatch_json.ts:42:11)
at Object.sendSync ($deno$/ops/dispatch_json.ts:69:10)
at Object.openSync ($deno$/ops/fs/open.ts:22:10)
at Object.openSync ($deno$/files.ts:28:15)
at Object.readFileSync ($deno$/read_file.ts:6:16)
at readFileStrSync (read_file_str.ts:18:30)
at parseApiFile (parseApiFile.ts:13:23)
at getSpecificationObject (getSpecificationObject.ts:16:24)
at swaggerDoc (index.ts:23:33)
at server.ts:41:21
No such file or directory (os error 2)
error: Uncaught Error: NotFound: No such file or directory (os error 2)
throw new Error(err);
^
at swaggerDoc (index.ts:38:11)
at server.ts:41:21
[denon] app crashed - waiting for file changes before starting ...

here's the repo

github.com/iampeters/deno-opine/bl...

Collapse
 
craigmorten profile image
Craig Morten • Edited

Hey Peters 😄 I fell into the same problems initially as well, the key is around apis array which you provide in the options object that is then passed to the swaggerDoc() method.

Unfortunately the swagger doc module doesn't perform auto discovery of files, you need to list them in the array. You also need to take care with the path used, as it expects the path to be relative to the current working directory where the deno run command is executed.

Looking at your example it looks like you still have references to Opine example files (REF: github.com/iampeters/deno-opine/bl...). Hopefully amending this array to mirror your repo should fix your problem. E.g. providing ./server.ts, and your paths under ./src/.

Thread Thread
 
iampeters profile image
Peters Chikezie

thanks for this. I have been able to get it to work. However, is there a swagger-UI that can be used in place of this JSON that is being served? with express, I can use swagger-UI-express and swagger-jsdoc to serve HTML to users. I am just curious.

Thread Thread
 
craigmorten profile image
Craig Morten

Unfortunately afaik this particular module is just for swagger JSON generation and doesn’t support the swagger UI as far as I know.

I don’t think there is a Deno module that can do this just yet - I might look to port the swagger UI node module so this can be done (unless someone has already done it!)

If you find something that can do it in Deno, please let me know!

Thread Thread
 
iampeters profile image
Peters Chikezie

oh ok. thanks. May I ask how you port these modules, I would love to learn so I can port some for myself 😜

Thread Thread
 
craigmorten profile image
Craig Morten

That would awesome for you to get involved! It depends very much on the module you wish to port.

  1. If it is already ESM and typescript, then you can just import it directly using the raw url!
  2. If it has no dependency on Node APIs in its code or subdeps then you can use JSPM CDN to import.
  3. Otherwise you need to port it - there are some tools to help, the rest is manually converting commonjs to ESM and swapping out Node methods for Deno ones.

Also check out my post on Reddit reddit.com/r/Deno/comments/h9ejsk/... for some more resources.

I might look to write a post on it if think would be useful?

Thread Thread
 
iampeters profile image
Peters Chikezie

thanks that would be awesome. I have been looking for a way to contribute to open-source but don't know where to start. You have been a great help so far.

Thread Thread
 
craigmorten profile image
Craig Morten

Generally it's a case of "I need to do a thing", seeing if it exists. If not then go code it and put it out there! If it helps you it'll help others 😄.

Check out things like firsttimersonly.com/ as well - there is a whole community worth of projects and issues geared towards helping people get involved in Open Source.

Collapse
 
iampeters profile image
Peters Chikezie

can it be integrated with swagger docs, I see swagger is available with Oak

Collapse
 
craigmorten profile image
Craig Morten

Ooh didn’t know that had supported that for Oak - do you have a link? I can’t find anything concrete but my Googling skills may be failing me 🙃 it certainly can be added if it doesn’t already work!

For instance, the deno_swagger_doc module should be immediately compatible. If you’re talking about generating server code from a swagger doc, that will take a little bit of work likely.

Collapse
 
iampeters profile image
Peters Chikezie • Edited

Deno_swagger_doc is the module I saw. The example is in Koa. I have tried to use opine to replicate it but I keep getting 404 error. This error is related to swagger_json. I will post the error here when I get to my laptop. I want users to be served the swagger docs when the hit / on my test deno server.

Thread Thread
 
craigmorten profile image
Craig Morten

I will look to add an example to the repo at some point later when have some free time!

Thread Thread
 
iampeters profile image
Peters Chikezie

Great... I'm looking forward to it.

Thread Thread
 
craigmorten profile image
Craig Morten

Sorry for the delay - check out the new swagger example here --> github.com/asos-craigmorten/opine/...

Thread Thread
 
iampeters profile image
Peters Chikezie

Great... Let me take a look