update: add morgan for http logging

This commit is contained in:
mahyargdz
2024-11-03 21:51:47 +03:30
parent 4e5fd392d1
commit a65d5173e6
4 changed files with 664 additions and 498 deletions
+47
View File
@@ -38,6 +38,7 @@
"moment-jalaali": "^0.9.2", "moment-jalaali": "^0.9.2",
"mongoose": "^5.12.5", "mongoose": "^5.12.5",
"mongoose-paginate-v2": "^1.3.18", "mongoose-paginate-v2": "^1.3.18",
"morgan": "^1.10.0",
"nodemailer": "^6.6.0", "nodemailer": "^6.6.0",
"nodemon": "^3.1.2", "nodemon": "^3.1.2",
"nuxt": "^2.15.3", "nuxt": "^2.15.3",
@@ -6293,6 +6294,24 @@
], ],
"license": "MIT" "license": "MIT"
}, },
"node_modules/basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"license": "MIT",
"dependencies": {
"safe-buffer": "5.1.2"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/basic-auth/node_modules/safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
"license": "MIT"
},
"node_modules/bcryptjs": { "node_modules/bcryptjs": {
"version": "2.4.3", "version": "2.4.3",
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
@@ -13299,6 +13318,34 @@
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/morgan": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz",
"integrity": "sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==",
"license": "MIT",
"dependencies": {
"basic-auth": "~2.0.1",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-finished": "~2.3.0",
"on-headers": "~1.0.2"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/morgan/node_modules/on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"integrity": "sha512-ikqdkGAAyf/X/gPhXGvfgAytDZtDbr+bkNUJ0N9h5MI/dmdgCs3l6hoHrcUv41sRKew3jIwrp4qQDXiK99Utww==",
"license": "MIT",
"dependencies": {
"ee-first": "1.1.1"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/move-concurrently": { "node_modules/move-concurrently": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz",
+1
View File
@@ -41,6 +41,7 @@
"moment-jalaali": "^0.9.2", "moment-jalaali": "^0.9.2",
"mongoose": "^5.12.5", "mongoose": "^5.12.5",
"mongoose-paginate-v2": "^1.3.18", "mongoose-paginate-v2": "^1.3.18",
"morgan": "^1.10.0",
"nodemailer": "^6.6.0", "nodemailer": "^6.6.0",
"nodemon": "^3.1.2", "nodemon": "^3.1.2",
"nuxt": "^2.15.3", "nuxt": "^2.15.3",
File diff suppressed because it is too large Load Diff
+53 -42
View File
@@ -1,16 +1,22 @@
const express = require('express'); const express = require("express");
const database = require('./database'); const database = require("./database");
const fileUpload = require('express-fileupload'); const fileUpload = require("express-fileupload");
const { isUser, isAdmin, isLoggedIn, hasPermission } = require('./authentication'); const morgan = require("morgan");
const bodyParser = require('body-parser'); const {
const cronJobs = require('./plugins/cronJobs'); isUser,
isAdmin,
isLoggedIn,
hasPermission,
} = require("./authentication");
const bodyParser = require("body-parser");
const cronJobs = require("./plugins/cronJobs");
const swaggerJsdoc = require("swagger-jsdoc"); const swaggerJsdoc = require("swagger-jsdoc");
const cors = require('cors') const cors = require("cors");
const swaggerUi = require("swagger-ui-express"); const swaggerUi = require("swagger-ui-express");
const { createAdmins } = require('./plugins/privateAdmin'); const { createAdmins } = require("./plugins/privateAdmin");
const portals = require('./plugins/portals'); const portals = require("./plugins/portals");
global._ = require('lodash'); global._ = require("lodash");
global.lodash = require('lodash-contrib'); global.lodash = require("lodash-contrib");
global.portal = []; global.portal = [];
for (const item of portals) { for (const item of portals) {
@@ -20,6 +26,11 @@ for (const item of portals) {
// Create express instance // Create express instance
const app = express(); const app = express();
app.set("trust proxy", 1);
app.disable("x-powered-by");
app.use(morgan("combined"));
const options = { const options = {
definition: { definition: {
openapi: "3.0.0", openapi: "3.0.0",
@@ -34,12 +45,12 @@ const options = {
components: { components: {
securitySchemes: { securitySchemes: {
bearerAuth: { bearerAuth: {
type: 'http', type: "http",
in: 'header', in: "header",
name: 'Authorization', name: "Authorization",
description: 'Bearer Token', description: "Bearer Token",
scheme: 'bearer', scheme: "bearer",
bearerFormat: 'JWT', bearerFormat: "JWT",
}, },
}, },
}, },
@@ -52,46 +63,46 @@ const options = {
}, },
], ],
}, },
apis: ['./server/routes/*.js'] apis: ["./server/routes/*.js"],
}; };
const specs = swaggerJsdoc(options); // const specs = swaggerJsdoc(options);
cronJobs(); cronJobs();
createAdmins(); createAdmins();
const corsOptions ={ const corsOptions = {
origin:'*', origin: "*",
credentials:true, credentials: true,
optionSuccessStatus:200, optionSuccessStatus: 200,
} };
// Init body-parser options (inbuilt with express) // Init body-parser options (inbuilt with express)
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
app.use(fileUpload()); app.use(fileUpload());
app.use(cors(corsOptions)) app.use(cors(corsOptions));
app.use( // app.use(
"/v1", // "/v1",
swaggerUi.serve, // swaggerUi.serve,
swaggerUi.setup(specs , {explorer:true}) // swaggerUi.setup(specs , {explorer:true})
); // );
// Require & Import API routes // Require & Import API routes
const Public = require('./routes/public'); const Public = require("./routes/public");
const admin = require('./routes/admin'); const admin = require("./routes/admin");
const auth = require('./routes/auth'); const auth = require("./routes/auth");
const user = require('./routes/user'); const user = require("./routes/user");
// Use API Routes // Use API Routes
app.use('/public', Public); app.use("/public", Public);
app.use('/auth', auth); app.use("/auth", auth);
app.use('/admin', [isLoggedIn, isAdmin], admin); app.use("/admin", [isLoggedIn, isAdmin], admin);
// app.use('/user', isUser, user); // app.use('/user', isUser, user);
// Error handling middleware to catch buffer issues // Error handling middleware to catch buffer issues
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
if (err.code === 'ENOBUFS') { if (err.code === "ENOBUFS") {
console.error('Buffer space error:', err); console.error("Buffer space error:", err);
res.status(500).send('Server is experiencing buffer space issues.'); res.status(500).send("Server is experiencing buffer space issues.");
} else { } else {
next(err); next(err);
} }
@@ -99,6 +110,6 @@ app.use((err, req, res, next) => {
// Export the server middleware // Export the server middleware
module.exports = { module.exports = {
path: '/api', path: "/api",
handler: app handler: app,
}; };