deno 介绍是安全的ts 运行时
简单的代码
- 使用js(app.js)
console.log("demoapp")
- 输出
dalongdemo
- 使用ts(app.ts)
console.log("demoapp")
- 输出
dalongdemo
导入模块
- 代码目录结构
├── rong.js
└── user
└── userlogin.ts
- 代码说明
user/userlogin.ts
export function userlogin(){
console.log("user login")
}
rong.js
import {userlogin } from "./user/userlogin.ts";
userlogin();
- 运行
deno rong.js
user login
url 模块导入
类似golang 的包格式
- 基本代码
remote module code
user.ts
export function userapp(){
console.log("userapp")
}
call remote module code
import {userapp} from "http://101.200.137.80/user.ts";
userapp();
- 输出结果
Downloading http://101.200.137.80/user.ts
userapp
发布&&订阅
内置的发布订阅模型
- pub 代码
import * as deno from "deno";
deno.sub("echo", (ui8: Uint8Array) => {
const str = String.fromCharCode.apply(null, ui8);
console.log("Got message", str);
});
function str2ui8(str: string): Uint8Array {
const ui8 = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
ui8[i] = str.charCodeAt(i);
}
return ui8;
}
console.log("Before deno.pub()");
deno.pub("echo", str2ui8("hello"));
console.log("After deno.pub()");
- 输出结果
Before deno.pub()
After deno.pub()
Got message hello
参考资料
https://github.com/ry/deno
https://github.com/ry/deno/tree/master/testdata