[JavaScript] Deno

Code/JavaScript 2020. 8. 14. 05:48

 

    

    JavaScript runtime, 

    - Node.js처럼 자바스크립트로 App 개발 가능



denoland / deno

https://deno.land/

https://github.com/denoland/deno - 66k

    - v1.2.2 , 2020/08

    - 2018/05 시작

    - v1.0 출시 , 2020/05




* 사용기술

    - V8 

    - Rust 

    - Tokio (Rust용 비동기 앱 런타임, 이벤트 루프에 사용)




//------------------------------------------------

* Hello World 샘플 코드

 

    - hello.ts 소스 코드

import { serve } from "https://deno.land/std@0.63.0/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" });

}

 

    - 실행

deno run --allow-net hello.ts



//---------------------------------------------

< Node.js와 차이점 >

 

* ES 모듈만 사용(원칙)

    - npm같은 패키지 매지저가 별도로 필요없음, 

        - node_modues 폴더 없음, package.json 파일 필요없음

 

    - 자바스크립트 표준 모듈(ES6 모듈)의 url을 직접 링크해서 사용(매니저 기능 내장)

 

    - Deno의 전역 개체를 사용하지 않으면 브라우저에서도 실행 가능

 

    - Node 모듈(NPM, ComnonJS (require))도 사용 가능

https://stackoverflow.com/questions/61821038/how-to-use-npm-module-in-deno

import { createRequire } from "https://deno.land/std/node/module.ts";

const require = createRequire(import.meta.url);

 

        

 

* 보안을 강화

    - OS 처럼 디자인 되어  디스크나 네트워크에 접근할때 권한을 요구

        - deno run --allow-net hello.ts 처럼 네트워크에 접근하려면  --allow-net  옵션이 필요



* 타입스크립트도 네이티브에서 실행



//---------------------------------------------

* 모듈 (라이브러리)

 

    - Standard Library

https://deno.land/std



    - Third Party Modules

https://deno.land/x




//-------------

*  NPM 을 ES 모듈로 변환 자료를 제공하는 사이트 (ESM CDN)

https://www.skypack.dev/ (구 Pika)

https://jspm.org/

https://unpkg.com 



//----------------------------------

    - 코드 샘플

https://deno.land/std/examples/

 

    - 샘플 - 채팅 서버

deno run --allow-net --allow-read https://deno.land/std/examples/chat/server.ts



    - 샘플 - 파일서버

deno install --allow-net --allow-read https://deno.land/std@0.63.0/http/file_server.ts



    - GUI 

 webview / webview_deno 

https://github.com/webview/webview_deno

deno run -A -r --unstable https://deno.land/x/webview/examples/local.ts




//---------------------

// 참고

https://blog.ull.im/engineering/2019/04/14/deno-ryan-dahl-2019-04-04.html

 

https://d2.naver.com/helloworld/7700312



Posted by 코덴스

댓글을 달아 주세요