-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (49 loc) · 1.57 KB
/
server.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
60
61
62
require('dotenv').config()
const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())
// parse requests of content-type - application/json
app.use(express.json())
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }))
// database
const db = require('./models')
const Category = db.category
const Product = db.product
const Material = db.material
Product.belongsToMany(Material, {
through: 'product_material',
as: 'materials',
foreignKey: 'product_id',
})
Material.belongsToMany(Product, {
through: 'product_material',
as: 'products',
foreignKey: 'material_id',
})
Category.hasMany(Material, { as: 'materials' })
Material.belongsTo(Category, {
foreignKey: 'category_id',
as: 'category',
})
Category.hasMany(Category, { as: 'children' })
Category.belongsTo(Category, {
foreignKey: 'parent_id',
as: 'parent',
})
const Init = require('./config/init/init')
db.sequelize.sync({ force: true }).then(async () => {
console.log('Drop and Resync Database with { force: true }')
await Init.categories.initialize(Category)
await Init.materials.initialize(Material)
await Init.products.initialize(Product)
})
app.listen(3000, () => console.log('Server started...'))
// Init routes
const productsRouter = require('./routes/products.route')
app.use('/products', productsRouter)
const categoriesRouter = require('./routes/categories.route')
app.use('/categories', categoriesRouter)
const materialsRouter = require('./routes/materials.route')
app.use('/materials', materialsRouter)