概要

前提知識

環境

Node.jsのインストール(yum)

$ sudo yum install nodejs
$ node -v
v0.10.42

MongoDBのインストール(yum)

# vi mongodb-org-3.2.repo
[mongodb-org-2.6]
name=MongoDB 2.6 Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
$ sudo yum install -y mongodb-org
$ mongo --version
MongoDB shell version: 3.2.6

スレッドモデルとイベントループ

Hello World

$ node
> console.log("hello world");
hello world
undefined
$ .help
console.log("hello world");
$ node hello.js
hello world

ノンブロッキング処理とブロッキング処理

// non blocking
setTimeout (function () {
	console.log("hello");
}, 1000);
console.log("world");
$ node hello.js
world
hello
// blocking
var start = new Date().getTime();
console.log("hello");
while (new Date().getTime() < start + 1000);
console.log("world");
$ node hello.js
hello
world

Webサーバーを作成

var http = require('http');
var server = http.createServer();
server.on('request', function(req, res) {
	res.writeHead(200, {'Content-TYpe': 'text/plain'});
	res.write('hello world');
	res.end();
});
server.listen(1337, '192.168.33.10');
console.log('server listening ...');
$ node server.js
server listening ...

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS