Skip to content

Instantly share code, notes, and snippets.

@blangue
Last active June 21, 2022 20:28
Show Gist options
  • Save blangue/c1ba8e1d2d590ec9163a4bc7b5cdb2b5 to your computer and use it in GitHub Desktop.
Save blangue/c1ba8e1d2d590ec9163a4bc7b5cdb2b5 to your computer and use it in GitHub Desktop.
HeaderParser microservice
// 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.ip, language: req.header('accept-language'), software: req.header('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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment