const express = require('express')
const morgan = require('morgan')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const cors = require('cors')

dotenv.config()

const app = express()
mongoose.connect(
  process.env.DATABASE, 
  { useNewUrlParser: true, useUnifiedTopology: true }, 
  (err) => {
    if (err) {
      console.log(err)
    } else {
      console.log('Connected to the database')
    }
  }
)

app.use(cors())
app.use(morgan('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

const productRouters = require('./routes/products')
const categoryRouters = require('./routes/category')
const ownerRouters = require('./routes/owner')
const userRouters = require('./routes/auth')


app.use('/api', productRouters)
app.use('/api', categoryRouters)
app.use('/api', ownerRouters)
app.use('/api', userRouters)

app.listen(3000, (err) => {
  if (err) {
    console.log(err)
  } else {
    console.log('start 3000')
  }
})