Last active
December 12, 2015 12:19
-
-
Save harbhub/4771548 to your computer and use it in GitHub Desktop.
This file contains 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
var timestamp=(function(){return (new Date()).getTime()})(); | |
var log=function(data){console.log('Servant : '+data+' : '+timestamp);}; | |
//var cap=function(string){string=(string).toString();return string.charAt(0).toUpperCase()+string.slice(1);}; | |
log('Activated'); | |
var http = require('http') | |
, fs = require('fs') | |
, ugly = require('uglify-js') | |
, mime = require('mime') | |
, formidable = require('formidable') | |
; | |
var handleRequest = function(req, res) { | |
log('Handling Request'); | |
if (req.url === '/') { | |
fs.createReadStream(__dirname+'/public/html/hub.html').pipe(res); | |
return; | |
} | |
else if (req.url === '/favicon.ico') { | |
fs.createReadStream(__dirname+'/public/ico/favicon.ico').pipe(res); | |
return; | |
} | |
else if (req.method.toLowerCase() === 'get') { | |
if (req.url.substr(0,7) === '/public') { | |
fs.stat(__dirname+req.url,function(err,stats){ | |
if (err) { | |
log('error retrieving file : file does not exist'); | |
res.end(); | |
return; | |
} | |
else { | |
fs.createReadStream(__dirname+req.url).pipe(res); | |
return; | |
} | |
}); | |
} | |
else { | |
log('error retrieving file : file is not public'); | |
res.end(); | |
return; | |
} | |
} | |
else if (req.method.toLowerCase() === 'post') { | |
log('post'); | |
log('action : '+req.url); | |
var form = new formidable.IncomingForm(); | |
var field = []; | |
form.uploadDir = '/tmp'; | |
form | |
.on('error', function(err) { | |
log('form error'); | |
}) | |
.on('field', function(field, value) { | |
log(field,value); | |
}) | |
.on('file', function(field, file) { | |
log(field, file); | |
}) | |
.on('end', function() { | |
log('form end'); | |
}); | |
form.parse(req); | |
} | |
/* | |
fs.stat(__dirname+req.url, function(err, stats){ | |
if (err) { | |
log('Stats : Error : '+err); | |
} | |
else { | |
log('Stats : Success : '+stats); | |
} | |
}); | |
*/ | |
}; | |
var servant=http.createServer(handleRequest); | |
servant.listen(8888); | |
/*HTML*/ | |
<html> | |
<head> | |
<title>Hub</title> | |
<script type='text/javascript'> | |
var log=function(data){console.log(data);}; | |
log('Start'); | |
</script> | |
</head> | |
<body> | |
<form action=""> | |
<label for="GET-name">Get Name:</label> | |
<input id="GET-name" type="text" name="name"> | |
<input type="submit" value="Save"> | |
</form> | |
<form action='/post' method="post"> | |
<label for="POST-name">Post Name:</label> | |
<input id="POST-name" type="text" name="name"> | |
<input id='POST-filename-upload' type='file' multiple='multiple'> | |
<input type="submit" value="Save"> | |
</form> | |
</body> | |
</html> | |
/*SERVER CONSOLE LOGS*/ | |
Servant : Activated : 1360689963674 | |
Servant : Handling Request : 1360689963674 | |
Servant : Handling Request : 1360689963674 | |
Servant : Handling Request : 1360689963674 | |
Servant : post : 1360689963674 | |
Servant : action : /post : 1360689963674 | |
Servant : name : 1360689963674 | |
Servant : form end : 1360689963674 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment