Monday, September 2, 2024

Boilerplate Project Header Parser NodeJS Express

// index.js
// where your node app starts

// init project
require('dotenv').config();
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({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/views/index.html');
});

// your first API endpoint...
app.get('/api/whoami', function (req, res) {
    res.json({
            ipaddress: req.get('x-forwarded-for'),
            language: req.get('accept-language'),
    software: req.get('user-agent')
    });
});

app.get('/api/test', function (req, res) {
    res.json({
        userAgent: req.get('user-agent')
    });
});


// listen for requests :)
var listener = app.listen(process.env.PORT || 3000, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});

views/index.html

<!DOCTYPE html>

<html>

   <head>
      <title>Request Header Parser</title>
      <link rel="shortcut icon" href="https://cdn.hyperdev.com/us-east-1%3A52a203ff-088b-420f-81be-45bf559d01b1%2Ffavicon.ico" type="image/x-icon"/>
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
      <link href="style.css" rel="stylesheet" type="text/css">
   </head>

   <body>
      <div class="container">
        <h2>Request Header Parser Microservice</h2>

        <h3>Example Usage:</h3>
        <p>
          <a href="api/whoami">[base url]/api/whoami</a>
        </p>

        <h3>Example Output:</h3>
        <p>
          <code>{"ipaddress":"::ffff:159.20.14.100","language":"en-US,en;q=0.5",<br>"software":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"}</code>
        </p>
      </div>
      <div class="footer">
        <p>
          by <a href="https://www.freecodecamp.org">freeCodeCamp</a>
        </p>
      </div>
   </body>


</html>

public/style.css

body {
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    color: #222;
    background-color: #FaFaFa;
    text-align: center;
    line-height: 1.4em;
}

.container {
    padding: 0;
    margin-top: 40px;
}

h3 {
  margin-top: 30px;
}

.footer {
    margin-top: 40px;
}

code {
  font-family: monospace;
  padding: 2px;
  color: black;
  background-color: #fff;
}

a {
    color: #2574A9;
}

readme.md

# Request Header Parser Microservice

This is the boilerplate for the Request Header Parser Microservice project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/request-header-parser-microservice

You should provide your own project, not the example URL.
Waiting: A request to /api/whoami should return a JSON object with your IP address in the ipaddress key.
Waiting: A request to /api/whoami should return a JSON object with your preferred language in the language key.
Waiting: A request to /api/whoami should return a JSON object with your software in the software key.


API Project: Request Header Parser Microservice
Example Usage:
[base url]/api/whoami
Example Output:

{"ipaddress":"159.20.14.100","language":"en-US,en;q=0.5",
"software":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"}