FreeCodeCamp-APIs and Microservices --- Request Header Parser Microservice
I built this microservice for freeCodeCamp's second APIs and Microservices Project.
Basically it accepts API requests and will respond with a JSON object containing the user's IP address, preferred language and information about their browser and operating system.
I used freeCodeCamp's Glitch boilerplate project. Here is a starter link.
We should use the Node.Js, Express and 'require-ip' for retrieving a request's IP address, also 'cors' for the freeCodeCamp tests for this project.
Let's start to write our main server.js file
If we don't use 'require-ip' packages the API will be standart answer for ip adress similar to this formats.
1-) If you're using localhost you'll see ;
127.0.0.1
2-) If you're using IPv4 you'll see;
::1
3-) If you're using IPv6 you'll see ;
::ffff:127.0.0.1
We can handle this problem with 'request-ip' npm packages.
Now it's time to HTTP GET request to the specified path ('/api/whoami') with the specified callback function.
After that time to serve static files such as images, CSS files, and JavaScript files, use the express.static
built-in middleware function in Express. And app.get requests to the specified path and res.sendFile
to provide fine-grained support for serving files.
Finally listen for connection on the specified host and port.
Actually this project was a little bit more simple than the Timestamp microservice project.
Here is the live version🦸♂️🦸♂️