AliMD / 1Tuts

:+1: Web Design Technology Tutorials
http://ali.md/tuts
62 stars 41 forks source link

C10 Node.js S1 #527

Closed AliMD closed 11 years ago

AliMD commented 11 years ago

Welcome

Salam, In class janbeye ashnayi ba class node.js ro dare mamoolan hame sare asp va php dava daran vali ma mihkaym begim bikhiyal har2 say mikonam bishtar farsi benevisam ta mafahim roshan beshe

Intro

Node.js be shoma ejaze mide ta network application haro ba javascript samte server besazid ! Yani: Server Side JavaScript ! Node.js az V8 JavaScript Runtime estefade mikone, Yani: Chrome JavaScript Engine ! (Fastest js engine)

Usage

Be che darde ma mikhore ?!

nabayad dar morede node.js eshtebah konid

Blocking Code

var contents = fs.readFileSync('/etc/hosts'); //1 (Freezing)
console.log(contents); //2

console.log('Doing something else'); //3

Non-Blocking Code

fs.readFile('/etc/hosts', function(err, contents) { //1
  console.log(contents); //3
});

console.log('Doing something else'); //2

or

var callback = function(err, contents) { //1
  console.log(contents); //3
};
fs.readFile('/etc/hosts', callback);

console.log('Doing something else'); //2

Blocking vs Non-Blocking

var callback = function(err, contents) {
  console.log(contents);
};
fs.readFile('file1', callback);
fs.readFile('file2', callback);

Read file 1 : 10s Read file 2 : 12s

Blocking : Read 2 file take 22s Non-Blocking : Read 2 file take 12s or little more

Hello Class

hello.js

var http = require('http'); // require http module

http.createServer(function(request, response) {
  response.writeHead(200); // Send status code to client (in header)
  response.write("Hello Class ;)"); // Send contnt
  response.end(); // Close the connection
}).listen(1234); // Listen for connections on port :1234

console.log("I'm Alive.");
node hello.js // run server
//> I'm Alive.
curl http://localhost:123
//> Hello Class ;)

The event loop

  1. Checking for Events
  2. Find request event
  3. Call my custom event
var http = require('http');
http.createServer(function(request, response) {
  // my custom event
}).listen(8080);
console.log('Listening on port 8080...');

Dar har zaman faghat yek event pardazesh mishe !

Another Sample

var http = require('http');

http.createServer(function(request, response) {
  response.writeHead(200);
  response.write("Hello Class ;)");

  setTimeout(function(){
    response.write("I'm Ali.MD !");
    response.end();
  }, 5000); // 5s

}).listen(1234);

Blocking vs Non-Blocking

Class Works

  1. Simple Hello server
  2. Convert Blocking
  3. Read File in Server
  4. Response WriteHead
  5. Send message in end

    Register

Baraye doostani ke tamayol be sherkat dar class ro darand, hanooz ham forsate sabte nam hast, http://ali.md/regclass

ehsansh commented 11 years ago

@AliMD pish niazesh chi hast ostad? albate Node.js tuie ali.md/regclass S3 hast gamunam inja eshteba neveshtin S1 ....?

ghost commented 11 years ago

heif js balad nistam :| vagarna ashegh iin node.js shodam !

AliMD commented 11 years ago

@ehsansh S1 yani jalase aval :D @mghayour jan ! toro che be node.js akhe ! aval js ro herfeyi shod bad

MOHS3N commented 11 years ago

Hello ! I'm Frist

mrmr68 commented 11 years ago

thanks sire ... :D

MOHS3N commented 11 years ago

man az haminja elam mikonam ke eftekhar mikonam az inke kenare jenabe aghaye @AliGH neshastam

AliGH commented 11 years ago

man Ham Az inke dar kenar Ostad Azim jenab @MOHS3N hastam , Hangam

artajalli commented 11 years ago

hey ... Thanks ... :)

jafarrasooli commented 11 years ago

hhi there

MOHS3N commented 11 years ago

o.

AliMD commented 11 years ago

Class Work 2

ye file index.html besazid ! va code zir ro be Non-blocking taghir bedid

var fs = require('fs');
var contents = fs.readFileSync('index.html');
console.log(contents);
AliMD commented 11 years ago

ClassWorks 1: Create simple server

var http = require('http');

http.createServer(function(request, response) {
  // Answer goes here.
}).listen(8080);
AliMD commented 11 years ago

ClassWork 2: Convert Blocking

ye file index.html besazid ! va code zir ro be Non-blocking taghir bedid

var fs = require('fs');
var contents = fs.readFileSync('index.html');
console.log(contents);
jafarrasooli commented 11 years ago

var fs = require('fs'); back = function( err , contents ){ console.log(contents+''); } fs.readFile('index.html',back(err , contents));

MOHS3N commented 11 years ago
var fs = require('fs');
var callback2 = function(error, contents) { 
  console.log(contents); 
};
fs.readFile('index.html', callback2);

console.log('ZendehAm');
mrmr68 commented 11 years ago

class work 2

var fs = require('fs');
fs.readFile('index.html',function(err,content){
    console.log(content+"");
});
console.log("man va to");
AliMD commented 11 years ago

ClassWork 3: Read File in Server

mesale balaro bejaye inke too console log kone, ye server besazin va be client befrestin ye chizi too in maye ha

var
  http = require('http'),
  fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200);

  response.end();
}).listen(8080);
MOHS3N commented 11 years ago
var
  http = require('http'),
  fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200);
  var html = fs.readFileSync('index.html');
  response.write(html);

  response.end();
}).listen(3030);

Ok is :end:

jafarrasooli commented 11 years ago
var
  http = require('http'),
  fs = require('fs');

http.createServer(function(request, response) {
  response.writeHead(200);
  fs.readFile('index.html',function(err,contents){
        response.write(contents);
        response.end();
  });

}).listen(1251);
mrmr68 commented 11 years ago

class work 3

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

    fs.readFile('index.html',function(err,content){
        res.write('man va to va ooo' + content);
        res.end();
    });

}).listen(4321);
AliMD commented 11 years ago

ClassWork 4, Response Write Head

writeHead mitoone parameter haye bishtari begire http://nodejs.org/docs/v0.6.18/api/http.html#http_response_writehead_statuscode_reasonphrase_headers mikham yek Content-Type barabare text/html ham be browser befrestid

AliMD commented 11 years ago

classwork hayi ke moondesh ro be onvane homework anjam bedid

ehsansh commented 11 years ago

@AliMD ostad man mitunam in dore ro biam? man bekham js bishtar iad begiram baiad biam hamin kelas ia S2?

MOHS3N commented 11 years ago

Az daste in Node.js Emshab be bi khabi mofrad dochaar shodam Va Be sorat kamelan tasadofi Az in Babat Khoshalam :trollface:

AliMD commented 11 years ago

@ehsansh nemidoonam cheghadr be js alaghe mand shodi vali alan bayad hadde aghal ye proje tejari besazi bad khodet raheto entekhab koni w4 va w5 baraye bahs haye mali barat lazem tare vali in class (s2) baraye yadgiriye shakhsi khoobe felan barat karborde maliye khassi nadare tasmim ba khodet

Mrshcom commented 11 years ago

khob ba arze poshesh babate dir hazer shodan! :dancer: hazeeeeeeeeeeeeeeeer! :D

MOHS3N commented 11 years ago

@AliMD man mikham ye site static ro ba node.js bala beyaram. OK?

 response.writeHead(200,{
        'Content-Type': 'text/html',

    });

vali natonestam img ha css ha ro load konam. toye console in error neshon midee

Resource interpreted as Image but transferred with MIME type text/html
Mrshcom commented 11 years ago

image ro nbayad khob type text bedi dg! image typesh text nis bara hamn in error ro mide!

MOHS3N commented 11 years ago

@Mrshcom Dorost megi mohammadReza , vali man nemitonam chanta Content-Type Benvisam , age chanta benevisam Hamishe Akharin 'Content-Type estefadeh mikone

ehsansh commented 11 years ago

@AliMD mamnun az rahnamaie. pas man hamun W4 ia W5 ro miam k lazemtare ta bad..... dostan bebakhshid vasate kelas umadam Good Luck :)

AliMD commented 11 years ago

Class be hade nesab sabte nam naresid, cancel

in term faghat 3 class mitoonam tashkil bedam

AliMD commented 11 years ago

@nasser-torabzade, @mrmr68, @jafarrasooli, @artajalli mohsen migoft azinke poshte sare jquery bad goftam kami delkhor shodin ! chon team khoobi hastin ino migam, yek developer hich vaght az taghir, pishraft va test rah haye jadid nemitarse. az tarafi in class hich rabti be jquery ya layout man dar css nadare !!! be har hal agar roozi nazaratam ro ghabool kardid man hamchenan dar khedmatam.

MOHS3N commented 11 years ago

@AliMD plz help me . manoo Nejad Bedeh az dast enna . Man nemikham Az Aghaedam Dast Bekesham.