Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const bodyParser = require('body-parser')
- const MongoClient = require('mongodb').MongoClient,
- assert = require('assert')
- const url = 'mongodb://127.0.0.1:27017/LogInApp'
- const app = express()
- app.use(express.static(__dirname));
- app.use(bodyParser.urlencoded({
- extended: true
- }))
- app.post('/registernewuser', (req, res) => {
- const user = {
- login: req.body.user.login,
- email: req.body.user.email,
- password: req.body.user.password
- }
- isThisValueOccupied('login', 'remes', MongoClient, 'users')
- /*insertDocument('users', user, MongoClient)
- .catch(err => console.log(err))
- .then(res.send('Registered'))*/
- })
- app.listen(3000, () => {
- console.log('Listening on port 3000!')
- })
- function insertDocument(collection, data, MongoClient) {
- return new Promise((resolve, reject) => {
- MongoClient.connect(url, (err, db) => {
- if (err) reject(err)
- db.collection(collection).insertOne(data)
- db.close()
- resolve()
- })
- })
- }
- function isThisValueOccupied(property, value, MongoClient, collection) {
- MongoClient.connect(url, (err, db) => {
- if (err) throw err
- const cursor = db.collection(collection).find({login: 'remes'})
- return cursor.count().then(data => data !== 0)
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement