Files
cloud-cap-samples/media-store/util/helpers.js

51 lines
1.2 KiB
JavaScript

const getDurationInMilliseconds = (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;
};
const getFormattedDateTime = () => {
let currentDateTime = new Date();
let formattedDateTime =
currentDateTime.getFullYear() +
"-" +
(currentDateTime.getMonth() + 1) +
"-" +
currentDateTime.getDate() +
" " +
currentDateTime.getHours() +
":" +
currentDateTime.getMinutes() +
":" +
currentDateTime.getSeconds();
return formattedDateTime;
};
const corsMiddleware = (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Methods",
"GET, PUT, PATCH, POST, DELETE, OPTIONS"
);
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Accept-Language"
);
//intercepts OPTIONS method
if ("OPTIONS" === req.method) {
//respond with 200
res.sendStatus(200);
} else {
//move on
next();
}
};
module.exports = {
getFormattedDateTime,
getDurationInMilliseconds,
corsMiddleware,
};