NODEJS


Node.js:Chrome V 8 JavaScriptエンジン構築JavaScriptランタイム


WebサーバWebサーバ:nodejs自体がWebサーバをサポート

var http = require('http');
var fs = require('fs');
var app = http.createServer(function(request,response){
    var url = request.url;
    if(request.url == '/'){
      url = '/index.html';
    }
    if(request.url == '/favicon.ico'){
        response.writeHead(404);
        response.end();
        return;
    }
    response.writeHead(200);
    response.end(fs.readFileSync(__dirname + url));
 
});
app.listen(3000);

CRUD


create
read
updat
delete

read

const fs = require('fs');
fs.readFile('sample.txt', 'utf8' , function(err , data){
    console.log(data);
});

conditional

var args = process.argv;
console.log(args[2]);
console.log('A');
console.log('B');
if(args[2] === '1'){
  console.log('C1');
} else {
  console.log('C2');
}
console.log('D');

ファイルリスト

var testFolder = './data/';
var fs = require('fs');

fs.readdir(testFolder, function(error, filelist){
      console.log(filelist);
  });

どうきひどうき


非同期:効率的だが複雑
var fs = require('fs');
 
/*
//readFileSync 동기
console.log('A');
var result = fs.readFileSync('syntax/sample.txt', 'utf8');
console.log(result);
console.log('C');
*/
 
//비동기
console.log('A');
fs.readFile('syntax/sample.txt', 'utf8', function(err, result){
    console.log(result);
});
console.log('C');

callback

// function a(){
//     console.log('A');
// }
var a= function (){
    console.log('A');
}


function slowfunc(callback){
    callback();
}

slowfunc(a);

module

var M = {
    v:'v',
    f:function(){
        console.log(this.v);
    }
}

module.exports = M;
var part = require('./mpart.js');
part.f();