Skip to content

Commit

Permalink
Added logging for index.js
Browse files Browse the repository at this point in the history
To diagnose delays on production initialization, timestamped logging is added.
  • Loading branch information
chrsrns committed Mar 8, 2024
1 parent 3d6843c commit 27cc12a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ const fakedata = require("./prisma/fake-data");
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

// For logging
require("console-stamp")(console);
const elapsed = require("elapsed-time-logger");

elapsed.start("init");

require("dotenv").config();

console.log("Configuring Express and CORS...");
elapsed.start("cors");
app.use(express.json());
app.use(cors());
elapsed.end("cors", "Express and CORS configured in");

// For testing, add intentional delay
// app.use(function(req, res, next) { setTimeout(next, 500) });

// Frontend Static
console.log("Adding routes to Express...");
elapsed.start("static");
const path = __dirname + "/src-frontend-react/build/";
app.use(express.static(path));
elapsed.end("static", "Static files added in");

elapsed.start("routes");
const auth = require("./routes/auth.routes.js");
app.use("/backend/auth", auth);

Expand Down Expand Up @@ -64,7 +77,10 @@ app.post("/backend/fakestaffuser", async (req, res) => {
res.status(500).json({ error: "An error occurred!" });
}
});
elapsed.end("routes", "Routed added in");

console.log("Adding error handling middleware...");
elapsed.start("errmiddleware");
// Define error handling middleware
app.use((err, req, res, next) => {
// Handle the error here, for example, send an error response to the client
Expand All @@ -74,11 +90,17 @@ app.use((err, req, res, next) => {
}
res.json({ error: "Something went wrong" });
});
elapsed.end("errmiddleware", "Error middleware added in");

console.log("Adding catch-all route...");
elapsed.start("catchall");
// Define a catch-all route at the end of your routes
app.all("*", (req, res) => {
res.redirect("/");
});
elapsed.end("catchall", "Catch-all middleware added in");

elapsed.end("init", "Server started in");

// function print(path, layer) {
// if (layer.route) {
Expand Down

0 comments on commit 27cc12a

Please sign in to comment.