-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (45 loc) · 1.82 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ===========================================
// Main Application File
// ===========================================
// This file initializes the Express application and sets up middleware, routes, and error handling.
// Key actions:
// 1. Set up Express app and middleware.
// 2. Connect to Firebase services.
// 3. Define routes for the application.
// 4. Implement error handling middleware.
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const dotenv = require("dotenv");
const ejsMate = require("ejs-mate");
const indexRouter = require("./routes/index");
const logger = require("./middleware/logger");
// Load environment variables from .env file
dotenv.config();
const app = express();
// Use ejs-mate as the view engine
app.engine("ejs", ejsMate);
app.set("views", path.join(__dirname, "views")); // Set the views directory
app.set("view engine", "ejs"); // Set EJS as the view engine
// Custom middleware to log requests using Winston
app.use((req, res, next) => {
logger.info(`${req.method} ${req.url}`);
next();
});
// Set up middleware
app.use(express.json()); // Parse incoming JSON requests
app.use(express.urlencoded({ extended: false })); // Parse URL-encoded data
app.use(cookieParser()); // Parse cookie header and populate req.cookies
app.use(express.static(path.join(__dirname, "public"))); // Serve static files from public directory
// Set up routes
app.use("/", indexRouter); // Use the indexRouter for root path
// Handle 404 errors
app.use((req, res, next) => {
res.status(404).render("errors/404", { title: "404 Not Found" });
});
// Error handling middleware
app.use((err, req, res, next) => {
logger.error(`Server error: ${err.message}`);
res.status(500).render("errors/500", { title: "500 Internal Server Error" });
});
module.exports = app;