Last active
June 9, 2017 10:31
-
-
Save romeosierra1/a72af8795eb2745981c6286363ff010f to your computer and use it in GitHub Desktop.
An example using moment.js, bluebird.js and node.js
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
let moment = require('moment'); | |
let http = require('http'); | |
let url = require('url'); | |
let bluebird = require('bluebird'); | |
let fs = bluebird.promisifyAll(require('fs')); | |
let visitorCount = 0; | |
let users = []; | |
if (visitorCount == 0) { | |
fs.readFileAsync('visitedUsers.json', 'utf8').then(function (data) { | |
users = users.concat(JSON.parse(data)); | |
users.forEach(user => { | |
visitorCount += user.visitCount; | |
}) | |
console.log(data); | |
console.log('Visitor Count ' + visitorCount); | |
}).catch(function (err) { | |
fs.unlinkAsync('visitedUsers.json') | |
.then(function () { | |
console.log('File deleted. New log file will be created'); | |
}) | |
.catch(function () { | |
console.log('File could not be deleted.'); | |
}) | |
}); | |
} | |
function createUser(_name) { | |
let user = { | |
name: _name, | |
visitCount: 1, | |
lastVisited: moment.now() | |
}; | |
users.push(user); | |
} | |
function createForm() { | |
return '<form action="register" method="get">User Name : <input type="text" name="name"><br><input type="submit" value="Submit"></form>'; | |
} | |
function getPostFix(_num) { | |
if (_num == 1) { | |
return 'st'; | |
} else if (_num == 2) { | |
return 'nd'; | |
} else if (_num == 3) { | |
return 'rd'; | |
} | |
else{ | |
return 'th'; | |
} | |
} | |
http.createServer((req, res) => { | |
let output = ''; | |
if (req.url.startsWith('/register')) { | |
let qData = url.parse(req.url, true).query; | |
console.log(qData); | |
if (qData.name != '') { | |
if (users.length > 0) { | |
let userExists = false; | |
console.log(qData.name); | |
users.forEach(user => { | |
if (user.name === qData.name) { | |
user.visitCount++; | |
user.lastVisited = moment.now(); | |
userExists = true; | |
} | |
}); | |
if (!userExists) { | |
createUser(qData.name); | |
} | |
} else { | |
createUser(qData.name); | |
} | |
visitorCount++; | |
fs.writeFileAsync('visitedUsers.json', JSON.stringify(users), function () { | |
return 'Success'; | |
}).then(success => { | |
console.log('File dumped.') | |
}).catch(err => { | |
console.log(err); | |
}) | |
} else { | |
output = 'No user defined. This visit will not be logged.<br>'; | |
} | |
output += 'You are ' + visitorCount + getPostFix(visitorCount) + ' visitor. <br>' + users.length + ' visitors have visited till now. <br>'; | |
users.forEach(user => { | |
output += user.name + ' visited for ' + user.visitCount + getPostFix(user.visitCount) + ' time ' + moment(user.lastVisited).fromNow() + '<br>'; | |
}) | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
res.write(createForm()); | |
res.write(output); | |
return res.end(); | |
} else { | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
res.write(createForm()); | |
return res.end(); | |
} | |
}).listen(8080); | |
/* | |
- Every time a new user enters username, it updates the global visitor count and then registers the user | |
- If an exisitng user enters the username, it updates the global visitor count and user's visit count alongwith its visit time | |
- The page displays the info which contains user, its visit count and its last visit time.*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment