Since the release of Deno 1.0, there have been a lot of articles about deno, most of which are about how to install Deno, what features it has, what are the similarities and differences between DenO and Node, and whether or not Deno is a replacement for Node. Talk is cheap. Show me the code! Is it not sweet to experience the pleasure brought by the development of deno and to create an “enterprise” application: denO-supermarket with deno?

Some pits common to denO

Before we go into action, here are a few of the pitfalls I encountered when I first got to know deno.

A problem with the location of the permission identifier

As we all know, deno is secure by default, which means that access to the network, reading and writing files are not allowed by default. For example, a file named index.ts contains the following contents:

import { serve } from "https://deno.land/[email protected]/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}
Copy the code

If you run deno run index.ts, an error is reported:

error: Uncaught PermissionDenied: network access to "0.0.0.0:8000", run again with the --allow-net flag
Copy the code

So we naturally add –allow-net at the end of the startup command, as follows:

deno run index.ts --allow-net
Copy the code

However, this still generates an error. –allow-net, –allow-read, deno run, deno run, deno run

deno run --alow-net index.ts
Copy the code

Why not switch places? If –allow-net comes after the file name, it is passed to the JS script, not to deno. For more information, check out this confused by Order of CLI Option Flags. Just remember: the permission flag must follow deno Run!

Because most of our front-end students seldom write background and do not know the importance of security, in order to avoid all kinds of permission problems, I suggest that when writing some practice projects, we directly use deno Run-a to enable all permissions. (This is just for debugging purposes, and should be used sparingly in a production environment!)

Unstable APIS

Because mongodb is used in the actual process, it is necessary to introduce the third-party module Mongo of Deno. However, an error will be reported when starting the project:

error: TS2339 [ERROR]: Property 'openPlugin' does not exist on type 'typeof Deno'.
Copy the code

I checked and found that the openPlugin method is not stable at present. By default, deno only provides stable apis. If you need to enable unstable apis, add the –stable flag. Such as:

deno run -A --unstable index.ts
Copy the code

If you want to know if there is A problem with unstable -A and –unstable. It’s been tested to be all right. As long as the identifier comes before the file name.

Another question is, which are stable apis and which are unstable apis? In fact, denO official documents have helped us to classify, the entrance address is:

  • Stable API documentation
  • Unstable API documentation

If you are in doubt about why — Unstable is working, you can print all the members of the Deno with the following method:

console.log(Object.keys(Deno).length)
Copy the code

Ts: deno run — Unstable index.ts: 117, deno run index.ts: 88. Note There are 88 stable apis, and 29 unstable apis.

Some tips for using deno

Switching from Node to deno required a shift in our development mindset. So, let’s take a look at some of deno’s different development techniques from Node’s.

How do I manage versions?

I was also confused at first: without package.json, how do YOU control which versions you depend on? For example, if we have 10 files that rely on [email protected], each file is imported using the following code:

 import { init, MongoClient } from 'https://deno.land/x/[email protected]/mod.ts'
Copy the code

But one day, I suddenly want to upgrade from 0.6.0 to 0.7.0. What then? The replacement of a file is easy to miss, of course, can also be global search batch replacement. But it’s not very efficient.

The official recommendation is to use the deps.ts file to import remote files and manage versions. (Of course, the file name does not have to be deps.ts, you can change it to something else). This is done by importing all of the remote dependencies in deps.ts and re-exporting them. Then other files can get the required dependencies from deps.ts.

All 10 files depend on mongo. If you change the file to deps.ts, it looks like this:

export * from 'https://deno.land/x/[email protected]/mod.ts'
Copy the code

Then those files that need to use Mongo are not imported directly from the remote, but imported from deps.ts as follows:

import { init, MongoClient } from '.. /pathTo/deps.ts';
Copy the code

If we need to upgrade, we can simply change the mongo address in demos.ts from 0.6.0 to 0.7.0.

In addition, similar to NPM, if the version number is not specified, that is, the version is not specified in the remote address, for example:

export * from 'https://deno.land/x/mongo/mod.ts'
Copy the code

The latest version of the dependency is installed by default.

How do I find some useful deno libraries?

With Node, you can look up some libraries on NPM. Deno has a similar platform and is currently split into two libraries, one official standard and one third-party. The Standard library can be found here: Deno Standard Modules. The Third Party library can be found here: Deno Third Party Modules

Actual combat: the use of denO to develop a add delete check change mall system

OK, with the above knowledge point, can be actual combat now. First, make sure you have deno 1.0 installed on your computer. Also, since mongodb is used, you need to install mongodb on your computer as well.

interface

Let’s take a look at the interface of our mall:

The sparrow has all the organs! It has the functions of adding, querying, deleting and modifying commodities. This is a typical REST API-style system.

The project structure

Then look at the project structure:

  • .deno_plugins: This is the dynamically linked library downloaded by the Mongo module, so don’t worry about it.
  • Congig /db.ts: This is the configuration file for connecting to mongodb. The current dead-write port number is 27017. If your mongodb port is not this one, you can change it in this file.
  • Controllers /goods.ts: This is the logical code that implements the controllers
  • Public /index.html: this is the front end static page, has nothing to do with deno, we just need to use denO to service the directory.
  • Deps.ts: Used to manage remote dependency libraries and re-export them to other files for use.
  • The server.ts entry file is similar to the app.js entry file we use with epress or KOA.

Depends on the choice of modules

Because the project involves the front and back ends, you will typically choose Express or KOA if you use Node. Similarly, when using denO, we have to choose the corresponding framework, otherwise HTTP services and route jumps are not easy to handle. For this type of framework on deno, the more popular star is Oak and ABC. Here we choose ABC.

Also, because mongodb is used, mongo needs to be introduced

conclusion

Ok, for the first experience of deno, I wrote this, the specific code here is not going to post, interested can go to Github to check:

  • deno-supermarket

If you have any questions, you can learn from them