-
-
Save synsh/91cf1099efcd75b273cfc24358f95e71 to your computer and use it in GitHub Desktop.
Revisions
-
shimondoodkin revised this gist
Aug 12, 2013 . 4 changed files with 16 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,4 +21,5 @@ http.createServer(function (req, res) { }); } }).listen(1337); console.log('Server running at http://127.0.0.1:1337/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,4 +32,5 @@ http.createServer(function (req, res) { } }).listen(1337); console.log('Server running at http://127.0.0.1:1337/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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") ); // 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); console.log('Server running at http://127.0.0.1:1337/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/ 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 -
shimondoodkin revised this gist
Aug 12, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(run it in your project folder): npm install --save somepackage -
shimondoodkin revised this gist
Aug 12, 2013 . 5 changed files with 71 additions and 71 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // 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 { 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,35 +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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,41 +0,0 @@ File renamed without changes. -
shimondoodkin revised this gist
Aug 12, 2013 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: 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 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 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 then you can use this package in your program like: var somepackage=require('somepackage') -
shimondoodkin revised this gist
Aug 12, 2013 . 3 changed files with 31 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/ -
shimondoodkin revised this gist
Aug 12, 2013 . 2 changed files with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // 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'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
shimondoodkin revised this gist
Aug 12, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. and you wont be able to connect if it runs on a server. console.log('Server running at http://127.0.0.1:1337/'); -
shimondoodkin revised this gist
Aug 12, 2013 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.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(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.url,true); if(purl.pathname=='/test') res.end('Test'); else -
shimondoodkin created this gist
Aug 12, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/'); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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);