Created
February 22, 2015 01:07
-
-
Save FredrikAugust/d118ef966844ce999f2c to your computer and use it in GitHub Desktop.
Node.js IO
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
// Require the filesystem module from Node.js | |
var fs = require("fs"); | |
// Create a new date at this exact moment | |
var date = new Date(); | |
// Retrieve the date, month, hour, minute and second | |
var dateDate = date.getDate(), | |
dateMonth = date.getMonth() + 1, // Since the getMonth method is 0-based we need to add one to make it 1-12 | |
dateHour = date.getHours(), | |
dateMinute = date.getMinutes(), | |
dateSecond = date.getSeconds(); | |
// Prepare the custom message to write to the file | |
var msg = "-----------------------------------------" + "\n" + process.argv[2] + "\n" + dateDate + "/" + dateMonth + " " + dateHour + ":" + dateMinute + ":" + dateSecond + "\n"; | |
// Declare an if statement to check if the arg 'view' is passed with the command | |
if (process.argv[2] == 'view') { | |
// If it is dump the entire content to the console | |
fs.readFile('log.log', 'utf-8', function (err, data) { | |
// Throw the error if there is oen | |
if (err) throw err; | |
// If not log the content to the console | |
console.log(data); | |
}); | |
// Else if the argument delete is passed with the command delete the entire file | |
} else if (process.argv[2] == 'delete') { | |
// Write an empty string to the file, this is the same as deleting the whole thing | |
fs.writeFile("log.log", "", function(err) { | |
// Throw the error if there is one | |
if (err) throw err; | |
}); | |
// If none of the above statements match, append the msg to the log file | |
} else { | |
// Append the msg to the log.log file | |
fs.appendFile("log.log", msg, function(err){ | |
// Throw the error if you encounter one | |
if (err) throw err; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment