-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (36 loc) · 1.45 KB
/
server.js
File metadata and controls
47 lines (36 loc) · 1.45 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
const express = require('express')
const sequelize = require('sequelize')
const dotenv = require('dotenv').config()
const cookieParser = require('cookie-parser')
const cors = require("cors");
const db = require('./models');
const { userRoutes, invoiceRoutes, profileRoutes } = require('./routes');
const { items: Item, invoices: Invoice, users: User, accounts: Account } = db
const PORT = process.env.PORT || 8000
const app = express()
//middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser())
app.use(cors())
Invoice.belongsTo(User, { foreignKey: 'senderId', as: 'sender' });
Invoice.belongsTo(User, { foreignKey: 'receiverId', as: 'receiver' });
// Invoice.hasMany(Item, { foreignKey: 'invoiceId', as: 'items' });
// Item.belongsTo(Invoice, { foreignKey: 'invoiceId' });
User.hasOne(Account, { foreignKey: 'userId', as: 'account' });
Account.belongsTo(User, { foreignKey: 'userId' });
//synchronizing the database and forcing it to false so we dont lose data
db.sequelize.sync({ force: false }).then(() => {
console.log("db has been re sync")
})
app.get('/', (req, res) => {
console.log('API server reached')
res.status(200).send('API server reached')
})
//routes for the auth API
app.use('/auth', userRoutes)
//routes for the invoice API
app.use('/invoice', invoiceRoutes)
app.use('/profile', profileRoutes)
//listening to server connection
app.listen(PORT, () => console.log(`Server is connected on ${PORT}`))