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”: “https://hyperdev.com/#!/project/welcome-project"
},
“keywords”: [
“node”,
“hyperdev”,
“express”
],
“license”: “MIT”
}
package.json

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 (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// 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 ;

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.

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

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’);
res.json({
ipadress: ipadress,
language:language[0],
software:software
});
});

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.use(express.static(‘public’));
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🦸‍♂️🦸‍♂️

https://bdev-fccheaderparser.glitch.me

--

--