Skip to content

Instantly share code, notes, and snippets.

@canfan
Created April 15, 2020 15:30
Show Gist options
  • Save canfan/997b3e9d3c5c5def76b2e3ec1e73c6e3 to your computer and use it in GitHub Desktop.
Save canfan/997b3e9d3c5c5def76b2e3ec1e73c6e3 to your computer and use it in GitHub Desktop.
simple HTTP post logger with UA based client identification
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const uas = {
index: [],
get(ua) {
let id = this.index.indexOf(ua);
if (id < 0) {
id = this.index.length;
this.index.push(ua);
console.log('-\nnew UA:', id, ua, '\n-');
}
return id;
},
};
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res) => {
const ua = uas.get(req.get('User-Agent'));
console.log(ua, req.body);
res.end();
});
app.listen(8383);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment