Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js入門

 Node.js入門

インストールからモジュールによる拡張まで!

badatmath

April 01, 2012
Tweet

More Decks by badatmath

Other Decks in Programming

Transcript

  1. var http = require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200,

    {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) }); )FMMP8PSME͔Β ϓϩάϥϛϯά
  2. var http = require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200,

    {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) }); ॻ͘ ςετ͢Δ मਖ਼͢Δ ϒϥ΢βͰςετ ςετͰָΛ͢Δ αʔόىಈ ʼʊ
  3. /PEFͰςετ ॻ͘ ࣗಈςετ मਖ਼͢Δ αʔόىಈ ʼʊ ςετͰָΛ͢Δ var http =

    require('http'); var server = http.createServer(function(req,res){ ! res.writeHead(200, {"Content-Type": "text/plain"}); ! res.end('Hello World'); }); server.listen(9000, function(){ ! server.on('connection', function(socket){ ! ! console.log('client connected'); ! }) });
  4. var http = require('http'); var cnt = 0; var logm

    = function(str){ console.log('\033[32m S: ' + str + '\033[39m'); }; var server = exports.server = http.createServer(function(req,res){ logm('transmitting response header'); res.writeHead(200, {"Content-Type": "text/plain"}); logm('transmitting response body'); res.end('Hello World'); }); server.on('connection', function(socket){ cnt++; logm('client connected'); logm('sum: '+cnt); }); /* 3rd party modules */ var hello = require('./mod_server'); /* standard modules */ var http = require('http'), assert = require('assert'); /* config */ var PORT = 9000, options = { host: 'localhost', port: PORT, path: '/', method: 'GET' }; /* variables */ var server_response = ''; /* functions */ var logm = function(str){ // yellow console.log('\033[33m C: ' + str + '\033[39m'); }; /* * Error log */ var loge = function(str){ // cyan console.error('\036[33m CError: ' + str + '\033[39m'); }; /* code starts here */ hello.server.listen(PORT, function(){ console.log('starting server kicking by client'); /* using http.get() is more easier */ var req = http.request(options, function(res){ logm('status: '+res.statusCode); assert.equal(200, res.statusCode, 'status code should be 200'); res.on('data', function(chunk) { logm('body: '+chunk); server_response += chunk; }); res.on('end', function(){ logm('server response ended'); hello.server.close(); }); }); //.on('error', function(e){console.error('>>>>');}).end(); req.end(); req.on('error', function(e){ loge('an error occured: '); loge(e); }); }); /* assert response */ process.on('exit', function(){ assert.equal('Hello World', server_response); console.log('\033[7m test passed \033[27m'); }); ςετͰָΛ͢Δ ղઆʂ
  5. ςετͰָΛ͢Δ ςετର৅ /* assert response */ process.on('exit', function(){ assert.equal('Hello World',

    server_response); console.log('\033[7m test passed \033[27m'); }); ऴྃ࣌ʹϨεϙϯεϘσΟͷ಺༰ΛνΣοΫ
  6. { ! "name": "firstApp", ! "version": "0.0.1", ! "dependencies": {

    ! ! "express": "" ! } } ֦ுͰύϫϑϧʹ ϞδϡʔϧͷΠϯετʔϧํ๏ package.jsonϑΝΠϧΛॻ͘ npm installίϚϯυͰΠϯετʔϧ Φεεϝʂ
  7. var app = exports.server = require('express').createServer(); app.get('/', function(req, res){ res.send('Hello

    World'); }); var http = require('http'); var server = exports.server = http.createServer(function(req,res){ res.writeHead(200, {"Content-Type": "text/plain"}); res.end('Hello World'); }); ֦ுͰύϫϑϧʹ ࢖ͬͯΈΑ͏
  8. /* 3rd party modules */ var hello = require('./mod_server2'); /*

    standard modules */ var http = require('http'), assert = require('assert'); /* config */ var PORT = 9000, options = { host: 'localhost', port: PORT, path: '/', method: 'GET' }; /* functions */ var logm = function(str){ // yellow console.log('\033[33m C: ' + str + '\033[39m'); }; /* * Error log */ var loge = function(str){ // cyan console.error('\036[33m CError: ' + str + '\033[39m'); }; var http_req = function(options, callback, close){ var server_response = ''; var req = http.request(options, function(res){ assert.equal(200, res.statusCode, 'status code should be 200, but got '+res.statusCode); res.on('data', function(chunk) { server_response += chunk; }); res.on('end', function(){ if(close){ hello.server.close(); } callback(server_response); }); }); req.end(); req.on('error', function(e){ loge('an error occured: '); loge(e); }); }; /* code starts here */ hello.server.listen(PORT, function(){ // 1st test options.path = "/"; http_req(options, function(res){ assert.equal(res, "Hello World"); }); // 2nd test options.path = "/hello"; http_req(options, function(res){ assert.equal(res, "Hello Hello"); }); // last test options.path = "/last"; http_req(options, function(res){ assert.equal(res, "Bye!"); }, 'close'); }); /* assert response */ process.on('exit', function(){ console.log('\033[7m test passed \033[27m'); }); var app = exports.server = require('express').createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); app.get('/hello', function(req, res){ res.send('Hello Hello'); }); app.get('/last', function(req, res){ res.send('Bye!'); }); ֦ுͰύϫϑϧʹ ςετ͔Βॻ͍͍ͯ͘ͱָͪΜ