Skip to content

Instantly share code, notes, and snippets.

@synsh
Forked from shimondoodkin/goals.md
Created November 4, 2018 16:01
Show Gist options
  • Select an option

  • Save synsh/91cf1099efcd75b273cfc24358f95e71 to your computer and use it in GitHub Desktop.

Select an option

Save synsh/91cf1099efcd75b273cfc24358f95e71 to your computer and use it in GitHub Desktop.

Revisions

  1. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 4 changed files with 16 additions and 5 deletions.
    3 changes: 2 additions & 1 deletion step2b.js
    Original file line number Diff line number Diff line change
    @@ -21,4 +21,5 @@ http.createServer(function (req, res) {
    });
    }

    }).listen(1337);
    }).listen(1337);
    console.log('Server running at http://127.0.0.1:1337/');
    3 changes: 2 additions & 1 deletion step3.js
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,5 @@ http.createServer(function (req, res) {

    }

    }).listen(1337);
    }).listen(1337);
    console.log('Server running at http://127.0.0.1:1337/');
    5 changes: 3 additions & 2 deletions step4.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ http.createServer(function(req, res) {
    var purl = url.parse(req.url, true);
    if (purl.pathname == '/hello') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('hello ' + (purl.query.name||"world") );
    res.end('hello ' + (purl.query.name||"world") ); // notice i use querystring arguments here http://127.0.0.1:1337/hello?name=me!
    } else {
    fs.exists(__dirname + purl.pathname, function(exists) {
    if (exists) {
    @@ -38,4 +38,5 @@ http.createServer(function(req, res) {

    }

    }).listen(1337);
    }).listen(1337);
    console.log('Server running at http://127.0.0.1:1337/');
    10 changes: 9 additions & 1 deletion step5.md
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,12 @@ then you can use this package in your program like:


    npm documentation:
    * https://npmjs.org/doc/
    * https://npmjs.org/doc/

    people usually don't use vanilla node.js
    * people use "async" module or some promises module. to write a less callback based code. and use simple parallel or serial logic or use async foreach or foreach limit for asynchronious batch processing.
    * people use "express" module it is a better equipd http request and http response and http server. it has `./node_modules/express/bin/express --help` command that can generate a basic boilerplate.
    * the basic structure of the https server callback is function(req,res,next){}
    * if you like that you see you process it otherwise you modify what you want and call next to ket the handler to process the request.
    * express has basic file server with mimes and everything.
    * people use "mysql" module for database
  2. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion step5.md
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ in the command prompt enter this new project folder and run:
    if you are laizy then just press many times Enter and thats it, or you can enter your information there.
    it will create package.json for you and node_modules folder.

    then you can use npm in your program like:
    then you can use npm in your program like(run it in your project folder):

    npm install --save somepackage

  3. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 5 changed files with 71 additions and 71 deletions.
    24 changes: 24 additions & 0 deletions step2b.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // i have orgenized the code and added mime for each condition seperately

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

    http.createServer(function (req, res) {

    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('Test');
    }
    else
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile('step3.js', function (err, data){
    if (err) throw err;
    res.end(data);
    });
    }

    }).listen(1337);
    21 changes: 16 additions & 5 deletions step3.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // orgenize the code and add mime for each condition seperately
    // check if a file exists - read the api docs to learn how to check if a file exists
    // there is a special variable in each module __dirname it contains the directory name of this file

    var http = require('http');
    var url = require('url');
    @@ -14,11 +15,21 @@ http.createServer(function (req, res) {
    }
    else
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile('step3.js', function (err, data){
    if (err) throw err;
    res.end(data);
    fs.exists(__dirname+purl.pathname, function (exists) {
    if (exists){
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile(__dirname+purl.pathname, function (err, data){
    if (err) throw err;
    res.end(data);
    });
    }
    else
    {
    res.writeHead(404, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('File Not Found');
    }
    });

    }

    }).listen(1337);
    56 changes: 31 additions & 25 deletions step4.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,41 @@
    // check if a file exists - read the api docs to learn how to check if a file exists
    // there is a special variable in each module __dirname it contains the directory name of this file
    // add mimes

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

    http.createServer(function (req, res) {
    http.createServer(function(req, res) {

    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('Test');
    }
    else
    {
    fs.exists(__dirname+purl.pathname, function (exists) {
    if (exists){
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile(__dirname+purl.pathname, function (err, data){
    if (err) throw err;
    res.end(data);
    });
    }
    else
    {
    res.writeHead(404, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('File Not Found');
    var purl = url.parse(req.url, true);
    if (purl.pathname == '/hello') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('hello ' + (purl.query.name||"world") );
    } else {
    fs.exists(__dirname + purl.pathname, function(exists) {
    if (exists) {

    var extention = purl.pathname.match(/\..+$/)[0];
    var mime = {
    '.js': 'text/javascript; charset=UTF-8',
    '.txt': 'text/plain; charset=UTF-8',
    '.html': 'text/html; charset=UTF-8',
    '.png': 'image/png',
    '.gif': 'image/gif',
    '.jpg': 'image/jpeg'
    }

    res.writeHead(200, {'Content-Type': mime[extention]});
    fs.readFile(__dirname + purl.pathname, function(err, data) {
    if (err) throw err;
    res.end(data);
    });

    } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('File Not Found');
    }
    });

    }

    }).listen(1337);
    41 changes: 0 additions & 41 deletions step5.js
    Original file line number Diff line number Diff line change
    @@ -1,41 +0,0 @@
    // add mimes

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

    http.createServer(function(req, res) {

    var purl = url.parse(req.url, true);
    if (purl.pathname == '/hello') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('hello ' + (purl.query.name||"world") );
    } else {
    fs.exists(__dirname + purl.pathname, function(exists) {
    if (exists) {

    var extention = purl.pathname.match(/\..+$/)[0];
    var mime = {
    '.js': 'text/javascript; charset=UTF-8',
    '.txt': 'text/plain; charset=UTF-8',
    '.html': 'text/html; charset=UTF-8',
    '.png': 'image/png',
    '.gif': 'image/gif',
    '.jpg': 'image/jpeg'
    }

    res.writeHead(200, {'Content-Type': mime[extention]});
    fs.readFile(__dirname + purl.pathname, function(err, data) {
    if (err) throw err;
    res.end(data);
    });

    } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('File Not Found');
    }
    });

    }

    }).listen(1337);
    File renamed without changes.
  4. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions step6.md
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,32 @@
    search in google:
    node.js modules

    https://github.com/joyent/node/wiki/modules

    to see the whole range of possibilities.

    #using npm:

    using npm:

    as you installed node.js npm is also installed. this is how to use it:

    when you start a new project
    make an empty folder and put there your file it maybe called index.js it is also the default file name for a module.
    make an empty folder and put there your file.
    it maybe named index.js, it is also the default file name for a module.


    in the command prompt enter this new project folder and run:

    npm init

    if you are laizy then just press many times Enter and thats it or you can enter your information.
    if you are laizy then just press many times Enter and thats it, or you can enter your information there.
    it will create package.json for you and node_modules folder.

    then you can use npm in your program like:

    npm install --save somepackage

    the you can use it in your program like:
    then you can use this package in your program like:

    var somepackage=require('somepackage')

  5. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 3 changed files with 31 additions and 4 deletions.
    1 change: 1 addition & 0 deletions goals.md
    Original file line number Diff line number Diff line change
    @@ -6,3 +6,4 @@ goals:
    * to learn read the documentation
    * to understand the whole concept
    * to use some api for hands on expirience
    * streach goal: learn basic npm usage.
    4 changes: 0 additions & 4 deletions step6
    Original file line number Diff line number Diff line change
    @@ -1,4 +0,0 @@
    search in google:
    node.js modules

    to see the whole range of possibilities.
    30 changes: 30 additions & 0 deletions step6.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    search in google:
    node.js modules

    to see the whole range of possibilities.

    #using npm:
    as you installed node.js npm is also installed. this is how to use it:

    when you start a new project
    make an empty folder and put there your file it maybe called index.js it is also the default file name for a module.


    in the command prompt enter this new project folder and run:

    npm init

    if you are laizy then just press many times Enter and thats it or you can enter your information.
    it will create package.json for you and node_modules folder.

    then you can use npm in your program like:

    npm install --save somepackage

    the you can use it in your program like:

    var somepackage=require('somepackage')


    npm documentation:
    * https://npmjs.org/doc/
  6. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 2 changed files with 6 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion step4.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // check if a file exists - read the api docs
    // check if a file exists - read the api docs to learn how to check if a file exists
    // there is a special variable in each module __dirname it contains the directory name of this file

    var http = require('http');
    var url = require('url');
    4 changes: 4 additions & 0 deletions step6
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    search in google:
    node.js modules

    to see the whole range of possibilities.
  7. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion step0.js
    Original file line number Diff line number Diff line change
    @@ -5,5 +5,5 @@ var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
    }).listen(1337, '127.0.0.1'); // remove the ip argument from listen. otherwise it will bind only to this ip address.
    }).listen(1337, '127.0.0.1'); // remove the ip argument from listen. otherwise it will bind only to this ip address. and you wont be able to connect if it runs on a server.
    console.log('Server running at http://127.0.0.1:1337/');
  8. @shimondoodkin shimondoodkin revised this gist Aug 12, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion step1.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ var http = require('http');
    var url = require('url');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    var purl=url.parse(req.query,true);
    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    res.end('Test'); // there is response.write and response.end
    // response.end('text') is response.write('text'), then response.end();
    2 changes: 1 addition & 1 deletion step2.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ var url = require('url');
    var fs = require('fs');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    var purl=url.parse(req.query,true);
    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    res.end('Test');
    else
  9. @shimondoodkin shimondoodkin created this gist Aug 12, 2013.
    8 changes: 8 additions & 0 deletions goals.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    The plan is to take a simple nodejs server.
    and add to it simple file serving capabilities.
    also to have a custom responce to a certien url. to be able to make a simple api in addition of serving files.

    goals:
    * to learn read the documentation
    * to understand the whole concept
    * to use some api for hands on expirience
    9 changes: 9 additions & 0 deletions step0.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    //go to nodejs.org and copy the simple hello world code to a file.
    // test it go with the browser to http://127.0.0.1:1337/

    var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
    }).listen(1337, '127.0.0.1'); // remove the ip argument from listen. otherwise it will bind only to this ip address.
    console.log('Server running at http://127.0.0.1:1337/');
    16 changes: 16 additions & 0 deletions step1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    // 1. go to nodejs.org api documentation and read how to use url.parse
    // 2. use parse url and show a different text for a custom url

    var http = require('http');
    var url = require('url');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    var purl=url.parse(req.query,true);
    if(purl.pathname=='/test')
    res.end('Test'); // there is response.write and response.end
    // response.end('text') is response.write('text'), then response.end();
    else
    res.end('Hello World\n');

    }).listen(1337);
    console.log('Server running at http://127.0.0.1:1337/');
    26 changes: 26 additions & 0 deletions step2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    // read in the api documentation about reading files.
    // read some file to the output
    //
    // there are many options actually.
    // files coud be read with a callback. - quite efficient and simple i will use this way.
    // files coud be read Sync as a return value from the function - blocking the program, this way the program wont be able serve others while waithing for the file to be read from disk.
    // a stream could be created and piped to the response stream - most cpu and memory efficient.

    var http = require('http');
    var url = require('url');
    var fs = require('fs');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    var purl=url.parse(req.query,true);
    if(purl.pathname=='/test')
    res.end('Test');
    else
    fs.readFile('step2.js', function (err, data) {
    if (err) throw err;
    // console.log(data);
    res.end(data);
    });


    }).listen(1337);
    console.log('Server running at http://127.0.0.1:1337/');
    24 changes: 24 additions & 0 deletions step3.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // orgenize the code and add mime for each condition seperately

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

    http.createServer(function (req, res) {

    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('Test');
    }
    else
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile('step3.js', function (err, data){
    if (err) throw err;
    res.end(data);
    });
    }

    }).listen(1337);
    34 changes: 34 additions & 0 deletions step4.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // check if a file exists - read the api docs

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

    http.createServer(function (req, res) {

    var purl=url.parse(req.url,true);
    if(purl.pathname=='/test')
    {
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('Test');
    }
    else
    {
    fs.exists(__dirname+purl.pathname, function (exists) {
    if (exists){
    res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
    fs.readFile(__dirname+purl.pathname, function (err, data){
    if (err) throw err;
    res.end(data);
    });
    }
    else
    {
    res.writeHead(404, {'Content-Type': 'text/plain; charset=UTF-8'});
    res.end('File Not Found');
    }
    });

    }

    }).listen(1337);
    41 changes: 41 additions & 0 deletions step5.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // add mimes

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

    http.createServer(function(req, res) {

    var purl = url.parse(req.url, true);
    if (purl.pathname == '/hello') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('hello ' + (purl.query.name||"world") );
    } else {
    fs.exists(__dirname + purl.pathname, function(exists) {
    if (exists) {

    var extention = purl.pathname.match(/\..+$/)[0];
    var mime = {
    '.js': 'text/javascript; charset=UTF-8',
    '.txt': 'text/plain; charset=UTF-8',
    '.html': 'text/html; charset=UTF-8',
    '.png': 'image/png',
    '.gif': 'image/gif',
    '.jpg': 'image/jpeg'
    }

    res.writeHead(200, {'Content-Type': mime[extention]});
    fs.readFile(__dirname + purl.pathname, function(err, data) {
    if (err) throw err;
    res.end(data);
    });

    } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('File Not Found');
    }
    });

    }

    }).listen(1337);