forked from jorgeberrizbeitia/express-server-basic-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (25 loc) · 1015 Bytes
/
server.js
File metadata and controls
32 lines (25 loc) · 1015 Bytes
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
// ℹ️ Loads environment variables from a .env file into process.env
try {
process.loadEnvFile()
} catch(error) {
console.warn(".env file not found, using default environment values")
}
// ℹ️ Establishes a connection to the database
require("./db");
// Imports Express (a Node.js framework for handling HTTP requests) and initializes the server
const express = require("express");
const app = express();
// ℹ️ Loads and applies global middleware (CORS, JSON parsing, etc.) for server configurations
const config = require("./config")
config(app);
// 👇 Defines and applies route handlers
const indexRouter = require("./routes/index.routes");
app.use("/api", indexRouter);
// ❗ Centralized error handling (must be placed after routes)
const handleErrors = require("./errors")
handleErrors(app);
// ℹ️ Defines the server port (default: 5005)
const PORT = process.env.PORT || 5005;
app.listen(PORT, () => {
console.log(`Server listening. Local access on http://localhost:${PORT}`);
});