ノードからDENOへ
41956 ワード
記事https://aralroca.com/blog/from-node-to-deno
先週、私はデノに関する記事を発表しましたChat app with Deno and Preact . それ以来、多くの疑問が生じた.それらのほとんどは、私たちがノードでしたのと同じことをする方法についてです、しかし、新しいdeno生態系で.
私はノードで最も使用されるトピックのいくつかを収集しようとしており、Denoとの代替案を探した.まず最初に、現在のノードの多くを使用できることを明確にしたい.jsモジュール.多くのモジュールが再利用可能であるため、すべての選択肢を探す必要はありません.訪問できますpika.dev DENUで使用するモジュールを探す.リストから始めましょう
以下をカバーします. Electron Forever / PM2 Express / Koa MongoDB PostgresSQL MySQL / MariaDB Redis Nodemon Jest, Jasmine, Ava... Webpack, Parcel, Rollup... Prettier NPM Scripts Nvm Npx Run on a Docker Run as a lambda Conclusion
電子
を返します.を使用してデスクトップアプリケーションを作成することができますElectron . 電子はWeb環境を動かすためのインターフェイスとしてクロムを使用します.しかし、我々は電子をdenoで使うことができますか?選択肢はありますか.
さて、今、電子はデノの下で実行されることができないからです.我々は選択肢を探さなければならない.錆で作られているので使えますweb-view rust bindings DENKOでdestkopアプリケーションを実行するには.
このように、我々はネイティブのWeb WebViewを使用することができますように多くのwebビューとして実行します.
Repo: https://github.com/eliassjogreen/deno_webview
永久に/PM 2
Forever and PM2 CLIツールは、与えられたスクリプトがデーモンとして連続的に実行されることを保証するためのツールです.永遠とは異なり、PM 2はより完全で、ロードバランサとしても機能します.両方ともノードで非常に役に立ちます、しかし、我々はdenoでそれらを使うことができますか?
永遠にノードのみを意図しているので、それを使用することは不可能です.一方、PM 2ではインタプリタを使用することができます.
エキスプレス
Express and Koa が最もよく知られているノードフレームワークです.彼らは堅牢なルーティングシステムとHTTPヘルパー(リダイレクション、キャッシュなど)で知られています.DENOでそれらを使用できますか?答えはそうではない.しかし、いくつかの選択肢があります.
http ( std lib )
denoの独自のstdライブラリはすでにExpressまたはKOAによって提供されるニーズの多くをカバーしています.https://deno.land/std/http/ .
オーク
今最もエレガントなソリューションの一つは、非常にコアに触発さ.https://github.com/oakserver/oak
ABC (サードリブ)
オークに似ている.https://deno.land/x/abc .
扶午急行
おそらくフレームワークを表現する最も類似した代替.https://github.com/NMathar/deno-express .
モンゴル語
MongoDB ドキュメントデータベースの巨大な安定性と柔軟性です.JavaScriptの生態系では、広く使用されている、平均またはmernのような多くのスタックでそれを使用します.とても人気があります.
それで、はい、我々はdenoでMongoDBを使うことができます.これを行うには、このドライバを使用できます.https://github.com/manyuanrong/deno_mongo .
PostgreSQL
MongoDBのように、ドライバもありますPostgresSQL .
https://github.com/buildondata/deno-postgres .
MySQL/MariADB
MongoDBとPostgreSQLの場合と同様に、MySQL / MariaDB .
https://github.com/manyuanrong/deno_mysql
レッドシス
Redis , キャッシングのための最もよく知られているデータベースは、デノのためのドライバもあります.
https://github.com/keroxp/deno-redis
ノデモン
Nodemon 開発環境では、ファイルの変更を監視し、サーバーを自動的に再起動します.これは、手動で停止し、アプリケーションの変更を表示するサーバーを再起動することなく、ノードの開発をはるかに楽しくなります.DENOで使用できますか?
すみませんが、できません.しかし、まだ代替手段があります:デンオン. https://github.com/eliassjogreen/denon 我々は使用するデンオンを使用することができます
ジャスミン、アバ.
ノード.JS生態系は、テストランナーのために多くの選択肢です.しかし、ノードをテストする公式の方法はありません.JSコード.
DENOでは、公式の方法があり、テスト用のstdライブラリを使用できます.
https://deno.land/std/testing
Webpack、小包、rolulup ...
denoの強みの1つは、私たちが、例えば、Webpack , Parcel or Rollup .
しかし、おそらく、ファイルのツリーを与えられた場合、私たちは、ウェブ上で実行するために1つのファイルにすべてを置くために束を作ることができるだろうか.
まあ、それは可能です、はい.我々は、denoのCLIでそれをすることができます.したがって、サードパーティ製のバンドルの必要はありません.
前途
ここ数年Prettier JavaScriptの生態系の中で非常によく知られているので、ファイルをフォーマットすることを心配する必要はありません.
そして、真実はそうです、それはまだdenoの上で使われることができます、しかし、denoがそれ自身のフォーマッタを持っているので、それはその意味を失います.
このコマンドを使ってファイルをフォーマットできます:
NPMスクリプト
DENOで
簡単な解決策は https://github.com/umbopepato/velociraptor スクリプトを使用してファイルを定義できます:
国立天文台
Nvm CLIは、複数のアクティブなノードのバージョンを管理するために、簡単にアップグレードまたはダウングレードのバージョンに応じて、プロジェクトに応じて.
エー
https://github.com/axetroy/dvm
国立天文台
Npx 近年では、NPMパッケージをインストールすることなく実行するために非常に人気となっている.現在、NPOの中には多くのプロジェクトが存在しない.そこで、どのようにしてDenoモジュールを実行することができますか
プロジェクトを実行するのと同じように、ファイルの代わりにモジュールのURLを指定します.
ドックに乗る
Docker内でdenoを実行するには、このdockerfileを作成します.
ラムダとして走る
DEMOをラムダとして使うには、deno - stdライブラリにモジュールがあります.https://deno.land/x/lambda .
デイン・インhttps://github.com/lucacasonato/now-deno
ええとhttps://blog.begin.com/deno-runtime-support-for-architect-805fcbaa82c3
結論
私はいくつかのノードのトピックとそれらのdenoの選択肢を忘れてしまった、私は私が説明するようにしたいことを逃した何かがあるかどうか私に知らせてください.私はこの記事はあなたがデノと氷を破ることができます願っています.
DENOで使用できるすべてのライブラリを探索するには、次の手順に従います. https://deno.land/std https://deno.land/x https://www.pika.dev/
先週、私はデノに関する記事を発表しましたChat app with Deno and Preact . それ以来、多くの疑問が生じた.それらのほとんどは、私たちがノードでしたのと同じことをする方法についてです、しかし、新しいdeno生態系で.
私はノードで最も使用されるトピックのいくつかを収集しようとしており、Denoとの代替案を探した.まず最初に、現在のノードの多くを使用できることを明確にしたい.jsモジュール.多くのモジュールが再利用可能であるため、すべての選択肢を探す必要はありません.訪問できますpika.dev DENUで使用するモジュールを探す.リストから始めましょう
以下をカバーします.
電子
を返します.を使用してデスクトップアプリケーションを作成することができますElectron . 電子はWeb環境を動かすためのインターフェイスとしてクロムを使用します.しかし、我々は電子をdenoで使うことができますか?選択肢はありますか.
さて、今、電子はデノの下で実行されることができないからです.我々は選択肢を探さなければならない.錆で作られているので使えますweb-view rust bindings DENKOでdestkopアプリケーションを実行するには.
このように、我々はネイティブのWeb WebViewを使用することができますように多くのwebビューとして実行します.
Repo: https://github.com/eliassjogreen/deno_webview
import { WebView } from "https://deno.land/x/webview/mod.ts";
const contentType = 'text/html'
const sharedOptions = {
width: 400,
height: 200,
resizable: true,
debug: true,
frameless: false,
};
const webview1 = new WebView({
title: "Multiple deno_webview example",
url: `data:${contentType},
<html>
<body>
<h1>1</h1>
</body>
</html>
`,
...sharedOptions,
});
const webview2 = new WebView({
title: "Multiple deno_webview example",
url: `data:${contentType},
<html>
<body>
<h1>2</h1>
</body>
</html>
`,
...sharedOptions,
});
await Promise.all([webview1.run(), webview2.run()]);
永久に/PM 2
Forever and PM2 CLIツールは、与えられたスクリプトがデーモンとして連続的に実行されることを保証するためのツールです.永遠とは異なり、PM 2はより完全で、ロードバランサとしても機能します.両方ともノードで非常に役に立ちます、しかし、我々はdenoでそれらを使うことができますか?
永遠にノードのみを意図しているので、それを使用することは不可能です.一方、PM 2ではインタプリタを使用することができます.
➜ pm2 start app.ts --interpreter="deno" --interpreter-args="run --allow-net"
エキスプレス
Express and Koa が最もよく知られているノードフレームワークです.彼らは堅牢なルーティングシステムとHTTPヘルパー(リダイレクション、キャッシュなど)で知られています.DENOでそれらを使用できますか?答えはそうではない.しかし、いくつかの選択肢があります.
http ( std lib )
denoの独自のstdライブラリはすでにExpressまたはKOAによって提供されるニーズの多くをカバーしています.https://deno.land/std/http/ .
import { ServerRequest } from "https://deno.land/std/http/server.ts";
import { getCookies } from "https://deno.land/std/http/cookie.ts";
let request = new ServerRequest();
request.headers = new Headers();
request.headers.set("Cookie", "full=of; tasty=chocolate");
const cookies = getCookies(request);
console.log("cookies:", cookies);
しかし、ルートを宣言する方法はあまり魅力的ではありません.それで、いくつかのより多くの選択肢を見ましょう.オーク
今最もエレガントなソリューションの一つは、非常にコアに触発さ.https://github.com/oakserver/oak
import { Application, } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
app.use((ctx) => {
ctx.response.body = "Hello World!";
});
await app.listen({ port: 8000 });
ABC (サードリブ)
オークに似ている.https://deno.land/x/abc .
import { Application } from "https://deno.land/x/abc/mod.ts";
const app = new Application();
app.static("/static", "assets");
app.get("/hello", (c) => "Hello!")
.start({ port: 8080 });
扶午急行
おそらくフレームワークを表現する最も類似した代替.https://github.com/NMathar/deno-express .
import * as exp from "https://raw.githubusercontent.com/NMathar/deno-express/master/mod.ts";
const port = 3000;
const app = new exp.App();
app.use(exp.static_("./public"));
app.use(exp.bodyParser.json());
app.get("/api/todos", async (req, res) => {
await res.json([{ name: "Buy some milk" }]);
});
const server = await app.listen(port);
console.log(`app listening on port ${server.port}`);
モンゴル語
MongoDB ドキュメントデータベースの巨大な安定性と柔軟性です.JavaScriptの生態系では、広く使用されている、平均またはmernのような多くのスタックでそれを使用します.とても人気があります.
それで、はい、我々はdenoでMongoDBを使うことができます.これを行うには、このドライバを使用できます.https://github.com/manyuanrong/deno_mongo .
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 = client.database("test");
const users = db.collection("users");
// insert
const insertId = await users.insertOne({
username: "user1",
password: "pass1"
});
// findOne
const user1 = await users.findOne({ _id: insertId });
// find
const users = await users.find({ username: { $ne: null } });
// aggregation
const docs = await users.aggregation([
{ $match: { username: "many" } },
{ $group: { _id: "$username", total: { $sum: 1 } } }
]);
// updateOne
const { matchedCount, modifiedCount, upsertedId } = await users.updateOne(
username: { $ne: null },
{ $set: { username: "USERNAME" } }
);
// deleteOne
const deleteCount = await users.deleteOne({ _id: insertId });
PostgreSQL
MongoDBのように、ドライバもありますPostgresSQL .
https://github.com/buildondata/deno-postgres .
import { Client } from "https://deno.land/x/postgres/mod.ts";
const client = new Client({
user: "user",
database: "test",
hostname: "localhost",
port: 5432
});
await client.connect();
const result = await client.query("SELECT * FROM people;");
console.log(result.rows);
await client.end();
MySQL/MariADB
MongoDBとPostgreSQLの場合と同様に、MySQL / MariaDB .
https://github.com/manyuanrong/deno_mysql
import { Client } from "https://deno.land/x/mysql/mod.ts";
const client = await new Client().connect({
hostname: "127.0.0.1",
username: "root",
db: "dbname",
poolSize: 3, // connection limit
password: "password",
});
let result = await client.execute(`INSERT INTO users(name) values(?)`, [
"aralroca",
]);
console.log(result);
// { affectedRows: 1, lastInsertId: 1 }
レッドシス
Redis , キャッシングのための最もよく知られているデータベースは、デノのためのドライバもあります.
https://github.com/keroxp/deno-redis
import { connect } from "https://denopkg.com/keroxp/deno-redis/mod.ts";
const redis = await connect({
hostname: "127.0.0.1",
port: 6379
});
const ok = await redis.set("example", "this is an example");
const example = await redis.get("example");
ノデモン
Nodemon 開発環境では、ファイルの変更を監視し、サーバーを自動的に再起動します.これは、手動で停止し、アプリケーションの変更を表示するサーバーを再起動することなく、ノードの開発をはるかに楽しくなります.DENOで使用できますか?
すみませんが、できません.しかし、まだ代替手段があります:デンオン.
deno run
スクリプトを実行する.➜ denon server.ts
ジャスミン、アバ.
ノード.JS生態系は、テストランナーのために多くの選択肢です.しかし、ノードをテストする公式の方法はありません.JSコード.
DENOでは、公式の方法があり、テスト用のstdライブラリを使用できます.
https://deno.land/std/testing
import { assertStrictEq } from 'https://deno.land/std/testing/asserts.ts'
Deno.test('My first test', async () => {
assertStrictEq(true, false)
})
テストを実行するには、次の手順に従います.➜ deno test
Webpack、小包、rolulup ...
denoの強みの1つは、私たちが、例えば、Webpack , Parcel or Rollup .
しかし、おそらく、ファイルのツリーを与えられた場合、私たちは、ウェブ上で実行するために1つのファイルにすべてを置くために束を作ることができるだろうか.
まあ、それは可能です、はい.我々は、denoのCLIでそれをすることができます.したがって、サードパーティ製のバンドルの必要はありません.
➜ deno bundle myLib.ts myLib.bundle.js
さあ、ブラウザにロードする準備ができました.<script type="module">
import * as myLib from "myLib.bundle.js";
</script>
前途
ここ数年Prettier JavaScriptの生態系の中で非常によく知られているので、ファイルをフォーマットすることを心配する必要はありません.
そして、真実はそうです、それはまだdenoの上で使われることができます、しかし、denoがそれ自身のフォーマッタを持っているので、それはその意味を失います.
このコマンドを使ってファイルをフォーマットできます:
➜ deno fmt
NPMスクリプト
DENOで
package.json
存在しない.私が本当に逃すことの1つは、中で宣言されたスクリプトですpackage.json
.簡単な解決策は
makefile
で実行するmake
. しかし、NPM構文を見逃すと、NPOスタイルのスクリプトランナーがDeno用です.# scripts.yaml
scripts:
start: deno run --allow-net server.ts
test: deno test --allow-net server_test.ts
で実行する➜ vr run <SCRIPT>
もう一つの選択肢はdenox , VeloCiraptorに非常に似ています.国立天文台
Nvm CLIは、複数のアクティブなノードのバージョンを管理するために、簡単にアップグレードまたはダウングレードのバージョンに応じて、プロジェクトに応じて.
エー
nvm
DEOに相当するdvm
.https://github.com/axetroy/dvm
➜ dvm use 1.0.0
国立天文台
Npx 近年では、NPMパッケージをインストールすることなく実行するために非常に人気となっている.現在、NPOの中には多くのプロジェクトが存在しない.そこで、どのようにしてDenoモジュールを実行することができますか
deno install https://url-of-module.ts
?プロジェクトを実行するのと同じように、ファイルの代わりにモジュールのURLを指定します.
➜ deno run https://deno.land/std/examples/welcome.ts
ご覧のように、モジュールの名前を覚えておく必要はありませんが、URL全体を覚えておく必要があります.一方で、私たちがどんなファイルも走らせることができて、より多くの柔軟性を与えますpackage.json
ライクnpx
.ドックに乗る
Docker内でdenoを実行するには、このdockerfileを作成します.
FROM hayd/alpine-deno:1.0.0
EXPOSE 1993 # Port.
WORKDIR /app
USER deno
COPY deps.ts .
RUN deno cache deps.ts # Cache the deps
ADD . .
RUN deno cache main.ts # main entrypoint.
CMD ["--allow-net", "main.ts"]
実行する➜ docker build -t app . && docker run -it --init -p 1993:1993 app
Repo: https://github.com/hayd/deno-docker ラムダとして走る
DEMOをラムダとして使うには、deno - stdライブラリにモジュールがあります.https://deno.land/x/lambda .
import {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context
} from "https://deno.land/x/lambda/mod.ts";
export async function handler(
event: APIGatewayProxyEvent,
context: Context
): Promise<APIGatewayProxyResult> {
return {
body: `Welcome to deno ${Deno.version.deno} 🦕`,
headers: { "content-type": "text/html;charset=utf8" },
statusCode: 200
};
}
面白い引用:結論
私はいくつかのノードのトピックとそれらのdenoの選択肢を忘れてしまった、私は私が説明するようにしたいことを逃した何かがあるかどうか私に知らせてください.私はこの記事はあなたがデノと氷を破ることができます願っています.
DENOで使用できるすべてのライブラリを探索するには、次の手順に従います.
Reference
この問題について(ノードからDENOへ), 我々は、より多くの情報をここで見つけました https://dev.to/aralroca/from-node-to-deno-5gpnテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol