-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (51 loc) · 2.02 KB
/
Copy pathapp.js
File metadata and controls
61 lines (51 loc) · 2.02 KB
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
57
58
59
60
61
const express = require("express");
const dotenv = require("dotenv");
dotenv.config();
const db = require("./models");
const errorHandler = require("./middleware/errorHandler");
// Import routes
const orderRoutes = require("./routes/order.routes");
const orderShareRoutes = require("./routes/orderShare.routes");
const app = express();
// Middleware to parse JSON bodies
app.use(express.json());
// Middleware to parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));
// Database synchronization
// In development, you might use { alter: true } or { force: true } to update/recreate tables.
// For production, migrations are preferred.
// The schema was provided, so we assume tables are created as per schema.
// `db.sequelize.sync()` will create tables if they don't exist based on model definitions.
// If your tables are already created by MySQL Workbench Forward Engineering, you might not need sync,
// or use it carefully (e.g., without force/alter options initially).
db.sequelize
.sync()
.then(() => {
console.log("Synced db.");
// Optionally, run seeders after sync if it's the first time or for development
if (
process.env.NODE_ENV === "development" &&
process.env.SEED_DB === "true"
) {
const seedMockData = require("./seeders/mockData");
console.log("Attempting to seed database...");
seedMockData().catch((err) => console.error("Seeding failed:", err));
}
})
.catch((err) => {
console.log("Failed to sync db: " + err.message);
});
// Simple route for testing server
app.get("/api", (req, res) => {
res.json({ message: "Welcome to the Order Management API." });
});
// API Routes
app.use("/api/orders", orderRoutes); // For creating orders, and potentially listing specific order by its own ID
app.use("/api/order-shares", orderShareRoutes);
// Global error handler - should be the last middleware
app.use(errorHandler);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
module.exports = app;