-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
82 lines (62 loc) · 2.32 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const dotenv = require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const {notFound,errorHandler} = require('./middleware/errorHandler')
const sequelize = require('./dbConnection')
const User = require('./models/User')
const Article = require('./models/Article')
const Tag = require('./models/Tag')
const Comment = require('./models/Comments')
const userRoute = require('./routes/users')
const articleRoute = require('./routes/articles')
const commentRoute = require('./routes/comments')
const tagRoute = require('./routes/tags')
const profileRoute = require('./routes/profile')
const favouriteRoute = require('./routes/favourites')
const app = express()
//CORS
app.use(cors({credentials: true, origin: true}))
//RELATIONS:
//1 to many relation between user and article
User.hasMany(Article,{
onDelete: 'CASCADE'
})
Article.belongsTo(User)
//many to many relation between article and taglist
Article.belongsToMany(Tag,{through: 'TagList',uniqueKey:false,timestamps:false})
Tag.belongsToMany(Article,{through: 'TagList',uniqueKey:false,timestamps:false})
//One to many relation between Article and Comments
Article.hasMany(Comment,{onDelete: 'CASCADE'})
Comment.belongsTo(Article)
//One to many relation between User and Comments
User.hasMany(Comment,{onDelete: 'CASCADE'})
Comment.belongsTo(User)
//Many to many relation between User and User
User.belongsToMany(User,{
through:'Followers',
as:'followers',
timestamps:false,
})
//favourite Many to many relation between User and article
User.belongsToMany(Article,{through: 'Favourites',timestamps:false})
Article.belongsToMany(User,{through: 'Favourites',timestamps:false})
const sync = async () => await sequelize.sync({alter:true})
sync()
app.use(express.json())
app.use(morgan('tiny'))
app.get('/',(req,res) => {
res.json({status:"API is running"});
})
app.use('/api',userRoute)
app.use('/api/articles',articleRoute)
app.use('/api/articles',commentRoute)
app.use('/api/tags',tagRoute)
app.use('/api/profiles',profileRoute)
app.use('/api/articles',favouriteRoute)
app.use(notFound)
app.use(errorHandler)
const PORT = process.env.PORT || 8080
app.listen(PORT,() => {
console.log(`Server running on http://localhost:8080`);
})