change gitignore. change server.js

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-11-29 21:26:21 +03:00
committed by Daniel Hutzel
parent 00474edffe
commit ee63541845
6 changed files with 64 additions and 10 deletions

View File

@@ -1,4 +1,8 @@
const cds = require("@sap/cds");
const {
getDurationInMilliseconds,
getFormattedDateTime,
} = require("./util/helpers");
// handle bootstrapping events...
cds.on("bootstrap", (app) => {
@@ -28,13 +32,26 @@ cds.on("bootstrap", (app) => {
cds.on("served", async ({ db, messaging, ...servedServices }) => {
// add logging current user before any request
for (let i in servedServices) {
servedServices[i].prepend((srv) =>
srv.before("*", (req) => {
servedServices[i].prepend((srv) => {
srv.on("*", async (req, next) => {
const method = req._.req.method;
const url = req._.req.url;
const start = process.hrtime();
if (req.user) {
console.log("[USER]:", req.user.id, req.user.attr, req.user._roles);
}
})
);
const result = await next();
const status = req._.res.statusCode;
const currentDateTime = getFormattedDateTime();
const durationInMilliseconds = getDurationInMilliseconds(start);
const log = `[${currentDateTime}] ${durationInMilliseconds.toLocaleString()} ms ${method}:${url} status: ${status} `;
console.log(log);
return result;
});
});
}
});