Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const MongoClient = require('mongodb').MongoClient,
  4.     assert = require('assert')
  5. const url = 'mongodb://127.0.0.1:27017/LogInApp'
  6. const app = express()
  7.  
  8. app.use(express.static(__dirname));
  9. app.use(bodyParser.urlencoded({
  10.     extended: true
  11. }))
  12.  
  13. app.post('/registernewuser', (req, res) => {
  14.  
  15.     const user = {
  16.         login: req.body.user.login,
  17.         email: req.body.user.email,
  18.         password: req.body.user.password
  19.     }
  20.    
  21.     isThisValueOccupied('login', 'remes', MongoClient, 'users')
  22.  
  23.     /*insertDocument('users', user, MongoClient)
  24.         .catch(err => console.log(err))
  25.         .then(res.send('Registered'))*/
  26.  
  27. })
  28.  
  29. app.listen(3000, () => {
  30.     console.log('Listening on port 3000!')
  31. })
  32.  
  33. function insertDocument(collection, data, MongoClient) {
  34.  
  35.     return new Promise((resolve, reject) => {
  36.         MongoClient.connect(url, (err, db) => {
  37.  
  38.             if (err) reject(err)
  39.             db.collection(collection).insertOne(data)
  40.             db.close()
  41.             resolve()
  42.  
  43.         })
  44.     })
  45.  
  46. }
  47.  
  48. function isThisValueOccupied(property, value, MongoClient, collection) {
  49.  
  50.     MongoClient.connect(url, (err, db) => {
  51.  
  52.         if (err) throw err
  53.         const cursor = db.collection(collection).find({login: 'remes'})
  54.         return cursor.count().then(data => data !== 0)
  55.     })
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement