|
| 1 | +--- |
| 2 | +layout: about.hbs |
| 3 | +title: Node.js とは |
| 4 | +trademark: Trademark |
| 5 | +--- |
| 6 | +# <!-- About Node.js® -->Node.js® とは |
| 7 | + |
| 8 | +<!-- |
| 9 | +As an asynchronous event driven JavaScript runtime, Node is designed to build |
| 10 | +scalable network applications. In the following "hello world" example, many |
| 11 | +connections can be handled concurrently. Upon each connection the callback is |
| 12 | +fired, but if there is no work to be done Node is sleeping. |
| 13 | +--> |
| 14 | + |
| 15 | +Node はスケーラブルなネットワークアプリケーションを構築するために設計された非同期型のイベント駆動の JavaScript 環境です。 |
| 16 | +以下の「Hello World」の例では、たくさんの接続を同時に処理することができます。 |
| 17 | +各接続ごとにコールバックは発火され、何もすることがない場合、Node はスリープします。 |
| 18 | + |
| 19 | +```javascript |
| 20 | +const http = require('http'); |
| 21 | + |
| 22 | +const hostname = '127.0.0.1'; |
| 23 | +const port = 3000; |
| 24 | + |
| 25 | +const server = http.createServer((req, res) => { |
| 26 | + res.statusCode = 200; |
| 27 | + res.setHeader('Content-Type', 'text/plain'); |
| 28 | + res.end('Hello World\n'); |
| 29 | +}); |
| 30 | + |
| 31 | +server.listen(port, hostname, () => { |
| 32 | + console.log(`Server running at http://${hostname}:${port}/`); |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +<!-- |
| 37 | +This is in contrast to today's more common concurrency model where OS threads |
| 38 | +are employed. Thread-based networking is relatively inefficient and very |
| 39 | +difficult to use. Furthermore, users of Node are free from worries of |
| 40 | +dead-locking the process, since there are no locks. Almost no function in Node |
| 41 | +directly performs I/O, so the process never blocks. Because nothing blocks, |
| 42 | +scalable systems are very reasonable to develop in Node. |
| 43 | +--> |
| 44 | + |
| 45 | +これは OS のスレッドが採用されている一般的な同時実行モデルとは対象的です。 |
| 46 | +スレッドベースのネットワーキングは比較的非効率であり、使うのはとても困難です。 |
| 47 | +さらに Node にはロックがないので Node ユーザーはプロセスのデッドロックの悩みから開放されます。 |
| 48 | +ほとんどの Node の関数は I/O を直接実行しないため、プロセスをブロックしません。 |
| 49 | +ブロックしないのでスケーラブルなシステムを開発するのに Node はとても最適です。 |
| 50 | + |
| 51 | +<!-- |
| 52 | +If some of this language is unfamiliar, there is a full article on |
| 53 | +[Blocking vs Non-Blocking][]. |
| 54 | +--> |
| 55 | + |
| 56 | +この言葉だけでは不慣れな部分がいくつかあるかもしれません。 |
| 57 | +[Blocking vs Non-Blocking][] にもう少し詳しい記事があります。 |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +<!-- |
| 62 | +Node is similar in design to, and influenced by, systems like Ruby's |
| 63 | +[Event Machine][] or Python's [Twisted][]. Node takes the event model a bit |
| 64 | +further, it presents an [event loop][] as a runtime construct instead of as a library. In other systems there is always a blocking call to start the |
| 65 | +event-loop. |
| 66 | +Typically behavior is defined through callbacks at the beginning of a script |
| 67 | +and at the end starts a server through a blocking call like |
| 68 | +`EventMachine::run()`. In Node there is no such start-the-event-loop call. Node |
| 69 | +simply enters the event loop after executing the input script. Node exits the |
| 70 | +event loop when there are no more callbacks to perform. This behavior is like |
| 71 | +browser JavaScript — the event loop is hidden from the user. |
| 72 | +--> |
| 73 | + |
| 74 | +Node は Ruby の [Event Machine][] や Python の [Twisted][] のシステムに影響を受けていて、同様の設計です。 |
| 75 | +Node はランタイムコンストラクタの替わりにライブラリとして[イベントループ][]を提供し、さらに小さなイベントモデルを持ちます。 |
| 76 | +ほかのシステムではイベントループの開始時にブロッキングコールが常にあります。 |
| 77 | +典型的な例ではスクリプトの先頭で動作をコールバックを用いて定義し、 |
| 78 | +最後に `EventMachine::run()` のようなブロッキングコールでサーバを起動します。 |
| 79 | +Node ではそのようなイベントループを開始する呼び出しはありません。 |
| 80 | +Node は単純にスクリプトを実行した直後にイベントループが開始されます。 |
| 81 | +実行するコールバックがこれ以上ない場合に Node はイベントループから抜けます。 |
| 82 | +この動作はブラウザ上の JavaScript と似ています — イベントループはユーザからは隠されます。 |
| 83 | + |
| 84 | +<!-- |
| 85 | +HTTP is a first class citizen in Node, designed with streaming and low latency |
| 86 | +in mind. This makes Node well suited for the foundation of a web library or |
| 87 | +framework. |
| 88 | +--> |
| 89 | + |
| 90 | +HTTP はストリーミングと低遅延を念頭に置いて設計された Node の第一級オブジェクトです。 |
| 91 | +これは Node で Web ライブラリやフレームワークの基礎を作るために適しています。 |
| 92 | + |
| 93 | +<!-- |
| 94 | +Just because Node is designed without threads, doesn't mean you cannot take |
| 95 | +advantage of multiple cores in your environment. Child processes can be spawned |
| 96 | +by using our [`child_process.fork()`][] API, and are designed to be easy to |
| 97 | +communicate with. Built upon that same interface is the [`cluster`][] module, |
| 98 | +which allows you to share sockets between processes to enable load balancing |
| 99 | +over your cores. |
| 100 | +--> |
| 101 | + |
| 102 | +Node はスレッドがない設計をしているという理由だけで、複数コアの利点が得られないわけではありません。 |
| 103 | +通信しやすく設計された子プロセスは [`child_process.fork()`][] API を使って生成できます。 |
| 104 | +コア上でロードバランシングを有効にするためにプロセス間でソケットを共有することを可能にする [`cluster`][] モジュールが同じインターフェース上に内蔵されています。 |
| 105 | + |
| 106 | +[Blocking vs Non-Blocking]: https://github.com/nodejs/node/blob/master/doc/topics/blocking-vs-non-blocking.md |
| 107 | +[`child_process.fork()`]: https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options |
| 108 | +[`cluster`]: https://nodejs.org/api/cluster.html |
| 109 | +[イベントループ]: https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md |
| 110 | +[Event Machine]: http://rubyeventmachine.com/ |
| 111 | +[Twisted]: http://twistedmatrix.com/ |
0 commit comments