FreeCodeCamp-APIs and Microservices — Request Header Parser Microservice

Barış Türe
2 min readDec 11, 2019


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.

“name”: “request_header”,
“version”: “0.0.1”,
“description”: “API project for freeCodeCamp”,
“main”: “server.js”,
“scripts”: {
“start”: “node server.js”
“dependencies”: {
“express”: “^5.0.0-alpha.2”,
“cors”: “^2.8.5”,
“request-ip”: “^2.1.3”
“engines”: {
“node”: “4.4.5”
“repository”: {
“type”: “git”,
“url”: “!/project/welcome-project"
“keywords”: [
“license”: “MIT”

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

// init project
var express = require(‘express’);
var app = express();
// enable CORS (
// so that your API is remotely testable by FCC
var cors = require(‘cors’);
app.use(cors({optionSuccessStatus: 200})); // some legacy browsers choke on 204

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 ;

2-) If you’re using IPv4 you’ll see;


3-) If you’re using IPv6 you’ll see ;


We can handle this problem with ‘request-ip’ npm packages.

const requestIp = require(‘request-ip’);
// inside middleware handler
var ipMiddleware = function(req, res, next) {
const clientIp = requestIp.getClientIp(req);
//As Connect Middleware

Now it’s time to HTTP GET request to the specified path (‘/api/whoami’) with the specified callback function.

app.get(‘/api/whoami’, (req, res) => {
var ipadress = req.clientIp;
var language = req.acceptsLanguages();
var software=req.get(‘User-Agent’);
ipadress: ipadress,

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.

app.get(“/”, function (req, res) {
res.sendFile(__dirname + ‘/views/index.html’);

Finally listen for connection on the specified host and port.

var listener = app.listen(process.env.PORT, function () {
console.log(‘Your app is listening on port ‘ + listener.address().port);

Actually this project was a little bit more simple than the Timestamp microservice project.

Here is the live version🦸‍♂️🦸‍♂️



Barış Türe

Software Developer👨🏻‍💻 Camper 🏕️

Recommended from Medium