Created
May 9, 2014 13:33
-
-
Save baniol/7d8f476d60b4f1bd6321 to your computer and use it in GitHub Desktop.
download file and log request with log4js, nodejs, http
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
{ | |
"appenders": [ | |
{ | |
"type": "file", | |
"filename": "serverLog.log" , | |
"category": "simplelog", | |
"backups" : 2, | |
"maxLogSize": 20480, | |
"description": "Example log." | |
}] | |
} |
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
{ | |
"first_name": "your name", | |
"surname": "your surname", | |
"ip": "your IP" | |
} |
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
{ | |
"name": "download_file", | |
"version": "0.0.1", | |
"description": "Example how to download file and log request.", | |
"dependencies": { | |
"log4js": "^0.6.14" | |
} | |
} |
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
http = require('http'), | |
url = require('url'), | |
path = require('path'), | |
log4js = require('log4js'); | |
log4js.configure('./cfg_log4js.json'); | |
var logger = log4js.getLogger('simplelog'); | |
var server = http.createServer(function (req, res) { | |
var file = __dirname + '/data.json'; | |
var filename = path.basename(file); | |
var query = url.parse(req.url, true).query; | |
logger.debug('username: ' + query.user + ', IP: ' + req.connection.remoteAddress); | |
fs.readFile(path.join('data.json'), 'utf8', function (err, data) { | |
res.setHeader('Content-disposition', 'attachment; filename=' + filename); | |
res.setHeader('Content-type', 'application/json'); | |
res.write(data); | |
res.end(); | |
}); | |
}); | |
server.listen(8080); |
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
_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment