FCC Backend Projects 1 and 2:
Time for some Backend projects this time since I am not yet ready for doing the Data-visualisation projects using d3.js
. The initial look at the projects seemed to be very easy. FCC already provides a Glitch boilerplate with Node and Express installed, and with some of the app’s routes already defined. Technically, I just had to write the logic for the specific API route.
Timestamp Microservice
The first Backend project using NodeJS and Express is to create a Timestamp Microservice. Given unix timestamp, I had to convert into Natural date and vice versa. A look at MDN’s documentation for the Javascript Date object gave away the solution.
I created a timestamp.js
module to bundle all the logic required to convert between the two Date formats and used it in the /:time
route. If time
is a number (i.e, a unix timestamp) use getFullYear()
, getMonth()
and getDate()
methods to convert it into a natural date format, otherwise use Date.parse()
method to convert the Date String to Unix timestamp.
Github Repository for this project is here. The project is live here.
Request Header Parser Microservice
This is another straightforward exercise in which we need to return some of the HTTP Request Headers. Refer to this MDN Documentation to understand about various types of Headers.
Getting the IP address
x-forwarded-for
property of request.headers
gives the IP address, if server is behind a proxy. It gives a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. Split the string and extract the leftmost value of IP address.
Other methods of obtaining the IP address based on whether the client is on HTTP or HTTPs, with or without proxy -
ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
Getting the Language
Language is obtained from req.headers[“accept-language”]
which returns a string of languages that the client can understand. For the desired output, we need to do some splitting and display only the first language.
Getting info about the Operating System
User’s system information is obtained using req.headers["user-agent"]
which again returns a String of values ( application type, operating system, software vendor or software version of the requesting software user agent). Again, after doing multiple split
operations on the String will give the required OS.
Github Repository for this project is here. The project is live here.
That’s all folks. See you in the next set of backend projects.