deno와 node.js 비교

deno로 만드는 서버

deno2020년 5월 14일 v1.0.0이 릴리즈 되었습니다. denonode를 비교하며 웹페이지에 Hello World를 띄워보도록 하겠습니다.

node!

우선 node를 이용하여 Hello World를 띄우도록 하겠습니다.

1
mkdir node && cd node && npm init -y

node폴더를 만들고 node플더로 이동하여 npm init으로 package.json파일을 생성합니다.

1
npm install express

npm으로 express를 설치합니다.

1
touch index.js

index.js 파일을 생성합니다.

1
2
3
4
5
6
7
8
const express = require('express');
const port = 8000;

const app = express();

app.get('*', (req, res) => res.send('Hello World'))

app.listen(port, () => console.log(`http://localhost:${port}`))

위 코드를 index.js에 작성합니다. npm으로 설치된 expressapp으로 실행하고 8000port를 이용하여 앱을 실행합니다.

1
node index.js

터미널에서 node를 이용하여 index.js를 실행합니다.

http://localhost:8000로 접속하면 Hello World를 확인 할 수 있습니다.

deno!

deno를 이용하여 Hello World를 띄우도록 하겠습니다.

1
mkdir deno && cd deno && touch index.ts

deno 폴더를 생성하고 deno 폴더로 이동하여 index.ts 파일을 생성합니다.

1
2
3
4
5
6
7
8
9
10
import { serve } from 'https://deno.land/std/http/server.ts';
const port = 8000;

const s = serve({ port });

console.log(`http://localhost:${port}`);

for await (const req of s) {
req.respond({ body: 'Hello World' });
}

위 코드를 index.ts에 작성합니다. https://deno.land/std/http/server.ts url을 이용하여 코드를 불러와 serve 함수를 받고 8000 포트를 사용하고, 응답을 bodyHello World로 보냅니다.

1
deno index.ts

denoindex.ts를 실행하면 error: Found argument 'index.ts' which wasn't expected, or isn't valid in this context 에러가 발생합니다. denonode와는 다르게 옵션을 추가하여 실행해야 합니다.

1
deno run --allow-net --allow-read index.ts

denodocker와 비슷하게 실행해야하는데 --allow-net 옵션으로 네트워크 통신을 허용해야하고 --allow-read 옵션으로 index.ts 파일을 읽을 수 있도록 허용해야합니다.
처음 deno로 파일을 실행하게되면 Compile file:///Users/username/Desktop/deno/index.ts와 같이 컴파일을 한 후 파일이 실행됩니다.

정리

denonode와는 다르게 package.json 파일, node_modules 폴더 등이 필요없고 url을 통해 직접 코드를 불러 올 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
deno -h
deno 1.0.0
A secure JavaScript and TypeScript runtime

Docs: https://deno.land/std/manual.md
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues

To start the REPL:
deno

To execute a script:
deno run https://deno.land/std/examples/welcome.ts

To evaluate code in the shell:
deno eval "console.log(30933 + 404)"

USAGE:
deno [OPTIONS] [SUBCOMMAND]

OPTIONS:
-h, --help Prints help information
-L, --log-level <log-level> Set log level [possible values: debug, info]
-q, --quiet Suppress diagnostic output
-V, --version Prints version information

SUBCOMMANDS:
bundle Bundle module and dependencies into single file
cache Cache the dependencies
completions Generate shell completions
doc Show documentation for a module
eval Eval script
fmt Format source files
help Prints this message or the help of the given subcommand(s)
info Show info about cache or info related to source file
install Install script as an executable
repl Read Eval Print Loop
run Run a program given a filename or url to the module
test Run tests
types Print runtime TypeScript declarations
upgrade Upgrade deno executable to given version

ENVIRONMENT VARIABLES:
DENO_DIR Set deno's base directory (defaults to $HOME/.deno)
DENO_INSTALL_ROOT Set deno install's output directory
(defaults to $HOME/.deno/bin)
NO_COLOR Set to disable color
HTTP_PROXY Proxy address for HTTP requests
(module downloads, fetch)
HTTPS_PROXY Same but for HTTPS

2020년5월16일 현재 deno의 문서는 아직까지 부족 한 것 같습니다. Docs: https://deno.land/std/manual.md 의 문서 역시 아래와 같이 링크가 깨져있어 볼 수 없습니다.

아직 v1.0.0이기 때문에 마이너 업데이트가 필요해 보입니다. 그래도 npm을 벗어난 개발이 매우 흥미롭습니다.

공유하기