const express = require('express');
const app = express();
require('dotenv').config(); // add dotnv for config
const server = require('http').createServer(app);
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const User = require('./db/models/User');

const PORT = process.env.PORT || 4000;
const HASH_KEY = process.env.HASH_KEY;


app.get('/', (req, res) => {
  res.send('Hello from express server!')
})

const db = async () => {
  try{
  const salt = await bcrypt.genSalt(2);
  const hashPassword = await bcrypt.hash('myPassword', salt);
  const user = new User({
    userName: 'Sergey',
    hashPassword: hashPassword
  })
  user.save();
  }catch(e){
    console.log(e)
  }

}

//db();

const start = async () => {
  try {
      await mongoose.connect('mongodb://localhost:27017/chat')
          .then(() => console.log(`DB started`))
      server.listen(PORT, () => {
          console.log(`Server started. Port: ${PORT}`);
      })
  } catch (e) {
      console.log(e);
  }
}

start();
