-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (43 loc) · 1.09 KB
/
app.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
// load environment variables
require('dotenv').config()
// imports
const express = require('express')
const cookieParser = require('cookie-parser')
const cors = require('cors')
// Router imports
const authRoutes = require('./routes/auth')
const usersRoutes = require('./routes/users')
const blogsRoutes = require('./routes/blogs')
// Database
const db = require('./db/database')
const Blog = require('./models/blog');
const User = require('./models/user')
User.hasMany(Blog, {
foreignKey: 'authorid'
})
db.authenticate()
.then(()=>{
console.log('DB Connected')
return db.sync(
// {force: true}
)
})
.then(()=>console.log('DB Synced'))
.catch(err=>{
console.error(`Error: ${err}`)
})
// Server
const app = express()
const port = process.env.PORT || 3000
// Middlewares
app.use(cors())
app.use(express.json())
app.use(cookieParser())
// Routes
app.use('/api/auth/', authRoutes)
app.use('/api/users/', usersRoutes)
app.use('/api/blogs/', blogsRoutes)
// Application server listen
app.listen(port, () => {
console.log(`Blogs API listening at http://localhost:${port}`)
})