Skip to content

Instantly share code, notes, and snippets.

@koad
Last active February 13, 2023 15:08
Show Gist options
  • Save koad/96f986be9f6dc2a2c486f91f563d3445 to your computer and use it in GitHub Desktop.
Save koad/96f986be9f6dc2a2c486f91f563d3445 to your computer and use it in GitHub Desktop.
process.stdout.write('\u001B[2J\u001B[0;0f'); // clear the screen
const signale = require('signale');
signale.time('upstart');
const upstart = new Date();
signale.pending('Catchall API starting...');
signale.info('Setting local timezone to "America/Toronto"');
process.env.TZ = 'America/Toronto';
const { parse } = require('querystring');
const express = require('express');
var request = require('request');
var url = require('url');
const app = express();
const localIP = "10.25.25.114";
const nginx = "consumer.example.ca"; // The domain name, if any, of the route nginx listens to. [false if none]
const port = 34211;
function displayInfo(req, body) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
let current_datetime = new Date();
let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds()
body = JSON.stringify(body, null, 4);
signale.success(`${req.method} from ${ip} to ${req.originalUrl}`);
signale.info(`time: ${current_datetime}`);
if( body != "{}" ) signale.log(`body: ${body}`);
};
app.all('*', (req, res) => {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
displayInfo(req, parse(body));
res.sendStatus(200);
});
});
var server = app.listen(port, localIP,() => {
var port = server.address().port;
signale.watch("App listening at http://%s:%s", server.address().address, port);
if(nginx) signale.watch("App secured at https://%s", nginx);
signale.timeEnd('upstart');
});
@koad
Copy link
Author

koad commented Aug 14, 2020

I often find myself needing a quick and dirty express api, .. maybe someone else might find this handy.//

/koad

@wizcreations7
Copy link

You know, I've never used entire document directly but I constantly come back to it to reference. At this point it's just force of habit...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment