index.js 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const express = require('express');
  2. const app = express();
  3. require('dotenv').config(); // add dotnv for config
  4. const server = require('http').createServer(app);
  5. const mongoose = require('mongoose');
  6. const bcrypt = require('bcrypt');
  7. const User = require('./db/models/User');
  8. const PORT = process.env.PORT || 4000;
  9. const HASH_KEY = process.env.HASH_KEY;
  10. app.get('/', (req, res) => {
  11. res.send('Hello from express server!')
  12. })
  13. const db = async () => {
  14. try{
  15. const salt = await bcrypt.genSalt(2);
  16. const hashPassword = await bcrypt.hash('myPassword', salt);
  17. const user = new User({
  18. userName: 'Sergey',
  19. hashPassword: hashPassword
  20. })
  21. user.save();
  22. }catch(e){
  23. console.log(e)
  24. }
  25. }
  26. //db();
  27. const start = async () => {
  28. try {
  29. await mongoose.connect('mongodb://localhost:27017/chat')
  30. .then(() => console.log(`DB started`))
  31. server.listen(PORT, () => {
  32. console.log(`Server started. Port: ${PORT}`);
  33. })
  34. } catch (e) {
  35. console.log(e);
  36. }
  37. }
  38. start();