From Node to Deno

Electron

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()]);

Forever / PM2

#!/bin/bash
deno run -A myCode.ts
➜ pm2 start ./app.sh

Express / Koa

Http (std lib)

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);

Oak (Third-party lib)

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 (Third party lib)

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 });

Deno-express (Third party lib)

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

import { init, MongoClient } from "https://deno.land/x/mongo@v0.6.0/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 });

PostgresSQL

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

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

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

➜ denon server.ts

Jest, Jasmine, Ava…

import { assertStrictEq } from 'https://deno.land/std/testing/asserts.ts'Deno.test('My first test', async () => {
assertStrictEq(true, false)
})
➜  deno test

Webpack, Parcel, Rollup…

➜ deno bundle myLib.ts myLib.bundle.js
<script type="module">
import * as myLib from "myLib.bundle.js";
</script>

Prettier

➜  deno fmt

NPM Scripts

# scripts.yaml
scripts:
start: deno run --allow-net server.ts
test: deno test --allow-net server_test.ts
➜  vr run <SCRIPT>

Nvm

➜  dvm use 1.0.0

Npx

➜  deno run https://deno.land/std/examples/welcome.ts

Run on a Docker

FROM hayd/alpine-deno:1.0.0EXPOSE 1993  # Port.WORKDIR /appUSER denoCOPY 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

Run as a 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
};
}

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Carbon Add Minutes In Laravel

What is the JAMstack?

Webpack 101

Add Specific Column updateMany ,upsert

Week 1 React

Authentication with Dj-rest-auth

Why Use Node.Js with React For Developing Web Apps?

Get started with ‘CodePush’ (React-Native)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aral Roca Gomez

Aral Roca Gomez

More from Medium

Let’s build a fresh project

Proximity based appliance switch in TypeScript

Monorepo vue 3 migration

Styling Remix App with Tailwind