• Modules you can use right now to build your first Deno Web app
  • Francesco Marassi
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: niayyy
  • Proofreader: Qicoo

Use 7 modules to build your first Deno Web application

Deno 1.0.0 is finally here. Here are some resources that will help you create your first Deno Web application.

What is Deno?

Deno was created by Ryan Dahl, and you may have heard of another project he created — yes, node.js.

Two years ago, Ryan gave a talk at JSConf titled “10 Things I Regret about Node.js,” where he announced that he was working on Deno, which would be a more secure version of Node, There is also no node_modules folder to make the project bloated.

Deno is a JavaScript/TypeScript runtime with default security features and a good developer experience, as you can see from the site deno.land (deno’s top-level domain name is a dinosaur mascot). It was built using V8, Rust, and Tokio.

The premise overview is great, but if you’re like me, you’ll want to get your hands on it to understand it better.

Here are 7 Deno modules to help you create a Web application and learn Deno:

1. Dinatra

If you’ve studied Ruby, you’ve probably heard of Sinatra, a small Web framework that allows you to build a Web server in five minutes with minimal effort.

Dinatra is Sinatra based on Deno, a lightweight Web application framework that you can now use to create your first Deno based Web application.

Here’s an example:

import {
  app,
  get,
  post,
  redirect,
  contentType,
} from "https://denopkg.com/syumai/[email protected]/mod.ts";

app(
  get("/hello".() = > "hello"),
  get("/hello/:id".({ params }) = > params.id),
  get(
    "/hello/:id/and/:name".({ params }) = > `:id is ${params.id}, :name is ${params.name}`,
  ),
  get("/error".() = > [500."an error has occured"]),
  get("/callName".({ params }) = > `Hi, ${params.name}! `),
  post("/callName".({ params }) = > `Hi, ${params.name}! `),
  get("/foo".() = > redirect("/hello".302)), // Redirect from /foo to /hello
  get("/info".() = > [
    200,
    contentType("json"),
    JSON.stringify({ app: "dinatra".version: "0.0.1"}),),);Copy the code

You can create a CRUD method that takes the parameters in the query and Body, and then returns all results in JSON format.

syumai/dinatra

2. Deno Postgres

import { Client } from "https://deno.land/x/postgres/mod.ts";

async function main() {
  const client = new Client({
    user: "user".database: "test".host: "localhost".port: "5432"
  });
  await client.connect();
  const result = await client.query("SELECT * FROM people;");
  console.log(result.rows);
  await client.end();
}

main();
Copy the code

With deno-Postgres, you can connect to your Postgres database and perform SQL queries. All methods return promises, so you can use await on all results.

buildondata/deno-postgres

3. Deno Nessie

Accessing and reading a Postgres database is fun, but you know what’s not? Change the database structure. Since there is no version control, a single error can destroy all of your stored data. Make sure you back up your data first, kids!

Deno-nessie is a deno-based database migration tool that will help you create migration files and change tables in your database. This way you can add your version to control all database change history and your colleagues will be able to easily change their local databases.

import { Schema } from "https://deno.land/x/nessie/mod.ts";

export const up = (scema: Schema): void= > {
  scema.create("users".(table) = > {
    table.id();
    table.string("name".100).nullable();
    table.boolean("is_true").default("false");
    table.custom("custom_column int default 1");
    table.timestamps();
  });

  scema.queryString(
    "INSERT INTO users VALUES (DEFAULT, 'Deno', true, 2, DEFAULT, DEFAULT);",); };export const down = (schema: Schema): void= > {
  schema.drop("users");
};
Copy the code

halvardssm/deno-nessie

4. Deno Mongo

Are you a NoSQL fan? Would you prefer to put all your data into a database with no fixed structure?

If you plan to use MongoDB in Deno, then Deno_mongo is what you need. Quickly add unstructured data to your repository 💪.

import { init, MongoClient } from "https://deno.land/x/[email protected]/mod.ts";

// Initialize the plugin
await init();

const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");

const db = getClient().database("test");
const users = db.collection("users");

// insert
const insertId = await users.insertOne({
  username: "user1".password: "pass1"
});

// insertMany
const insertIds = await users.insertMany([
  {
    username: "user1".password: "pass1"
  },
  {
    username: "user2".password: "pass2"}]);// findOne
const user1 = await users.findOne({ _id: insertId });

// find
const users = await users.find({ username: { $ne: null}});// count
const count = await users.count({ username: { $ne: null}});Copy the code

manyuanrong/deno_mongo

5. Deno SMTP

Most of the time, you’ll be sending mail in a Web application. If you need to send an email, here are some examples:

  • Confirmation emails for new users;
  • Forgotten password email;
  • Receiving subscription content;
  • ‘Do you miss us’ email, when the user hasn’t used your app for more than 7 days (this is the worst, please don’t Do this).

If you need to send those important emails, denO-SMTP is what you need and it will help you with your daily activities. You can also add HTML to your email content!

import { SmtpClient } from "https://deno.land/x/smtp/mod.ts";

const client = new SmtpClient();

await client.connect({
  host: "smtp.163.com".port: 25.username: "username".password: "password"});await client.send({
  from: "[email protected]".to: "[email protected]".subject: "Mail Title".content: "Mail Content, maybe HTML"});await client.close();
Copy the code

manyuanrong/deno-smtp

6. Deno Dotenv

You’ll start writing your shiny new Deno application on your own machine, with many of its modules using some sort of credentials (SMTP certificate, MongoDB URL, ECT…). . But you can’t put these credentials directly into your code because:

  • On production servers you will use different credentials (or at least I hope so);
  • You don’t want to expose them in some warehouse.

Deno-dotenv allows setting the. Env file. Put your credentials and environment variables in the.env file and use this module to get them:

# .env
GREETING=hello world

# main.ts
import { config } from "https://deno.land/x/dotenv/mod.ts";

console.log(config());
Copy the code

This is going to print out

> deno dotenv.ts
{ GREETING: "hello world" }
Copy the code

pietvanzoen/deno-dotenv

7. Denon

If you’re working with Node, you’ve probably used Nodemon to automatically reload your local server when saving files you’re writing.

Denon is such a module based on Deno.

You first need to use Deno Install to install

deno install --unstable --allow-read --allow-run -f 
https://deno.land/x/denon/denon.ts
Copy the code

You can then use Denon in your application file path to launch your local application. The next time you modify your application, Denon will automatically reload your Deno server!

eliassjogreen/denon

In addition to these, many NPM libraries are already compatible with Deno!

Yes, many NPM libraries are already compatible with Deno! This is the latest feature: write code once and run in both Node and Deno.

For example, you can now import and use these libraries in Deno:

www.i18next.com/overview/ge…

github.com/adrai/enum

Many other libraries will be fully Deno compatible next month.

Will Deno replace Node.js in the future?

It is too early to give a definite reply. Deno has reached version 1.0.0, but is far from complete.

Node will be the back-end JavaScript of choice for years to come, but it’s great to have a safer and correct alternative to one of JavaScript’s most awkward parts (yes, I’m talking about node_modules that eat too much hard disk space).

These modules will undoubtedly help you write your first Web application using Deno, and will help make the community around this new runtime even more powerful.

Any questions about Deno? Let me know on Twitter or leave a comment under this article!

The related resources

  • Want to learn more about Deno? Deno.land /manual/ intR…
  • Want to find more great modules based on Deno? Github.com/denolib/awe… There are many other modules to explore and use ✨

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.