// Global Variables
require('dotenv').config()

// Express
const express = require('express')
const compression = require('compression')
const app = express()
app.set('view engine', 'pug')
app.use(compression())
app.use(express.static('public'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use('/', require('./routers/home'))
const PORT = process.env.PORT || 3000
const server = app.listen(PORT, () => console.log(`zhao server running at port ${PORT}`))

// WebSocket
const io = require('socket.io')(server)
io.on('connection', (client) => { })

// MongoDB
const mongoose = require('mongoose')
const MONGODB_URL = process.env.MONGODB_URL || 'mongodb://localhost/test'
mongoose.connect(MONGODB_URL, { useNewUrlParser: true }).catch(error => console.error(error))
mongoose.connection.on('connected', () => console.info('mongodb connected'))
mongoose.connection.on('error', (error) => console.error(error))
