Skip to content

Commit

Permalink
--add: db logs, compression, helmet, allow headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ritiksr25 committed Dec 30, 2020
1 parent 1ff8cfa commit e2c05ad
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
30 changes: 24 additions & 6 deletions config/dbconnection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
const mongoose = require("mongoose");
require("dotenv").config();
const { logger } = require("../utility/helpers");
const { SERVER_ERROR } = require("../utility/statusCodes");
const { NODE_ENV, MONGO_URI } = require("./index");

// Map global promises
mongoose.Promise = global.Promise;
// Mongoose Connect
const connectionString = process.env.MONGO_URI;

// Debugg mongo
if (NODE_ENV === "development") mongoose.set("debug", true);

// Mongoose Connect
connectDb = async () => {
try {
await mongoose.connect(connectionString, { useNewUrlParser: true });
console.log("MongoDB Connected");
await mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
});
console.info("MongoDB Connected");
} catch (err) {
console.log(err);
console.info(err);
logger(
"error",
"database",
{
message: err.message,
stack: err.stack,
status: err.status || SERVER_ERROR
},
err
);
}
};
connectDb();
56 changes: 45 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const compression = require("compression");
const helmet = require("helmet");
const { NODE_ENV, PORT } = require("./config/index");
const { logRequestMiddleware } = require("./middlewares/log");

const app = express();
Expand All @@ -10,15 +12,31 @@ require("dotenv").config();
require("./config/dbconnection");

app.use(logRequestMiddleware);
app.use(compression());
app.use(helmet());
app.use(
cors({
exposedHeaders: "x-auth-token"
})
);
app.use(express.static(path.join(__dirname, "public")));
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
app.use(
bodyParser.json({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);

if (NODE_ENV === "production") {
console.log = console.warn = console.error = () => {};
}

// load schemas
const User = require("./models/User");
Expand All @@ -41,13 +59,29 @@ app.use("/api/v1/donate", require("./routes/api/v1/donate"));
// welcome cum not found route
app.use("/", require("./controllers/index_controller").welcome);

// Allowing headers
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Credentials", true);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
next();
});

//Setting up server
startServer = async () => {
(async () => {
try {
await app.listen(process.env.PORT);
console.log(`Server is up and running on Port ${process.env.PORT}`);
await app.listen(PORT);
console.info(
`NODE_ENV: ${NODE_ENV}\nServer is up and running on Port ${PORT}`
);
} catch (err) {
console.log("Error in running server.");
console.info("Error in running server.");
}
};
startServer();
})();
2 changes: 1 addition & 1 deletion middleware/log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const chalk = require("chalk");
const { formatHrTime } = require("../utility/helpers");
const { formatHrTime } = require("../utility/helpers.js");

module.exports.logRequestMiddleware = (req, res, next) => {
const startTime = new Date();
Expand Down

0 comments on commit e2c05ad

Please sign in to comment.