I am making a website, and I have Javascript code located directly in the HTML with the script tags. I want to log the IP addresses to a blank text file located in log/logfile.txt. I have a script to capture the time and IP address of the user, and here it is:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$.get("https://ipinfo.io", function(response) {
var ip = response.ip
}, "json")
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
var data = response.ip+' Connected at '+dateTime;
</script>
I want to write to the log file on my web server, but I don't know how. I've looked for similar question here but have found no answer. I've tried
const fs = require('fs')
fs.writeFile('log/logfile.txt', data, (err) => {
if (err) throw err;
})
And again, it doesn't work. Any help resolving this problem would be greatly apreciated.