-
Notifications
You must be signed in to change notification settings - Fork 341
Timestamp Microservice
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Medium Website | E-Mail
Build a full stack JavaScript app that successfully reverse-engineers this: http://votingapp.herokuapp.com/ and deploy it to Heroku.
Note that for each Basejump, you should create a new GitHub repository and a new Heroku project. If you can't remember how to do this, revisit http://freecodecamp.com/challenges/get-set-for-basejumps.
As you build your app, you should frequently commit changes to your codebase. You can do this by running git commit -am "your commit message"
. Note that you should replace "your commit message" with a brief summary of the changes you made to your code.
You can push these new commits to GitHub by running git push origin master
, and to Heroku by running grunt --force && grunt buildcontrol:heroku
.
- I can pass a string as a parameter, and it will check to see whether that string contains either a unix timestamp or a natural language date (example: January 1, 2016.
- If it does, it returns both the Unix timestamp and the natural language form of that date.
- If it does not contain a date or Unix timestamp, it returns null for those properties.
I used Momentjs which is a library for dealing with time and many different formats.
Initially I was just using plain Date
which is also fine, but you will have to make your own conversion for months, days and so on for the natural time.
I decided to create two functions that will convert from Unix to natural and from natural to Unix, then have a pair of check to handle the scenarios where the string given via url is Unix by checking if it is a number. Given that the Unix time is basically milliseconds passed since Jan 01 1970\. (UTC)
till now it must be minimum 0
thus any negative number should return a null value. Likewise if the string does not parse to a number then it must be a natural date format, so I just had to check if it validates to a date with Momentjs.
Something that I learned from there unrelated to dates is how to get queries without requiring the ?
in the url for APIs.
app.get('/:query', function(req, res) {
var date = req.params.query;
the :query
part is a variable name used to get whatever is after the /
that can be accessed via the req.params
as noted above.
I was also able to learn how to separate my API routes from the main routes which while not needed for this app, is something useful for more complex apps.
This is done by adding module.exports = function(app) {
tot he file with the route and by requiring
it on the server file like this var api = require('./app/api/timestamp.js');
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3