Created
October 18, 2015 02:36
-
-
Save heriipurnama/5bfcd1d6ba1cc8022051 to your computer and use it in GitHub Desktop.
queryString
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 characters
/** | |
* Created by heriipurnama on 18/10/15. | |
*/ | |
//queriString dari addresBAr | |
var http = require("http"); | |
var fs = require('fs'); | |
var url = require ('url'); | |
var gString = require("querystring"); | |
http.createServer(function(req,res){ | |
if(req.url != "/favicon.ico") { | |
var acces = url.parse(req.url);//1.parse dulu dg req.url | |
if (acces.pathname == "/"){ | |
var data = gString.parse(acces.query);//2.parse modul query | |
res.writeHead(200,{"Content-Type":"text/plain"}); | |
res.end(JSON.stringify(data)); | |
}else if(acces.pathname == "/form"){ | |
if(req.method.toUpperCase()=="POST"){ | |
//POST | |
var data_post = ""; | |
req.on('data',function(chunck){ | |
data_post += chunck; | |
}); | |
req.on('end',function(){ | |
console.log(data_post); | |
res.writeHead(200,{"Content-Type":"text/plain"}); | |
res.end(data_post); | |
}); | |
}else{ //GET | |
res.writeHead(200,{"Content-Type":"text/html"}); | |
fs.createReadStream("./template/form.html").pipe(res); | |
} //text perbandingan | |
}else{ | |
res.writeHead(200,{"Content_Type":"text/plain"}); | |
res.end("Page NOt Found !!!!"); | |
} | |
/*var data = gString.parse(acces.query);//2.parse modul query | |
//console.log(data.nama); //memangil satu-satu query | |
//console.log(data.status); | |
/*res.end(); | |
/*untuk melakukan qString beberapa validasi | |
1.validasi url dg modul url (untuk mendapatkan path-nya) | |
2.ambil query dg modul query string | |
*/ | |
/*========== awal routing =================== | |
var file = "/"; | |
if(acces.pathname == "/"){ | |
file = "./template/index.html" | |
}else if (acces.pathname=="/contac"){ | |
file = "./template/contac.html"; | |
}else{ | |
file = "./template/404.html" | |
} | |
res.writeHead(200, {"Content-Type": "text/html"}); | |
fs.createReadStream(file).pipe(res); | |
=========== akhir routing===================*/ | |
} | |
}).listen(8888); | |
console.log("server running di 8888"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment