Skip to content

Instantly share code, notes, and snippets.

@iitsuraj
Created March 3, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iitsuraj/0970e3e2fa18f9585d7e0fbcee33fa6e to your computer and use it in GitHub Desktop.
Save iitsuraj/0970e3e2fa18f9585d7e0fbcee33fa6e to your computer and use it in GitHub Desktop.
const express = require("express");
const fs = require("fs");
const PORT = 5000;
const app = express();
const getActualRequestDurationInMilliseconds = start => {
const NS_PER_SEC = 1e9; // convert to nanoseconds
const NS_TO_MS = 1e6; // convert to milliseconds
const diff = process.hrtime(start);
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
};
let demoLogger = (req, res, next) => { //middleware function
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();
let method = req.method;
let url = req.url;
let status = res.statusCode;
const start = process.hrtime();
const durationInMilliseconds = getActualRequestDurationInMilliseconds(start);
let log = `[${formatted_date}] ${method}:${url} ${status} ${durationInMilliseconds.toLocaleString()} ms`;
console.log(log);
fs.appendFile("request_logs.txt", log + "\n", err => {
if (err) {
console.log(err);
}
});
next();
};
app.use(demoLogger);
app.get("/", (req, res) => {
res.send("This is the home Page");
});
app.get("/about", (req, res) => {
res.send("This is the About page");
});
app.post("/add", (req, res) => {
res.send("Post request");
});
app.listen(PORT, () => {
console.log(`App is listening to port ${PORT}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment