-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (46 loc) · 1.86 KB
/
index.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
57
58
59
const express = require("express");
const cors = require("cors");
// Constants
const PORT = process.env.PORT || 80;
// Create express Main application
const app = express();
// Common express middleware
// Each middleware is triggered for every single request.
// Parsing incoming data.
app.use(express.urlencoded({
extended: true
}));
app.use(express.json());
// Cors enabled for *. From development only.
app.use(cors())
// Sync database with server
const db = require("./app/db");
try {
db.sequelize.sync(/*{force:true}*/);
} catch (err) {
console.log("There's been an error while syncing the database. Probably the connection failed.");
}
// Simple route for testing purposes
app.get("/", (req, res) => {
res.send("Room Leaves API");
})
// Initializing express router modules
require("./app/routes/categories.routes")(app);
require("./app/routes/plants.routes")(app);
require("./app/routes/users.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/orders.routes")(app);
require("./app/routes/scores.routes")(app);
require("./app/routes/favorites.routes")(app);
db.plants.belongsTo(db.categories); // categoryId
db.users.hasOne(db.orders, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.orders.hasOne(db.orderDetails, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.plants.hasOne(db.orderDetails, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.plants.hasOne(db.scores, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.users.hasOne(db.scores, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.plants.hasOne(db.favorites, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
db.users.hasOne(db.favorites, { foreignKey: { allowNull: false }, onDelete: 'CASCADE' });
// Listen for requests
app.listen(PORT, () => {
console.log(`Server running on port ${ PORT }`);
});