@Vandesm14/

router

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const http = require("http");
const {URL} = require("url");
var userExsists = "truee";
var namee;
var passworde;
var uestring; 

const server = http.createServer((req, res) => {
	let url = new URL(req.url, "http://dummy"); // placeholder url
	path = url.pathname;
	params = url.searchParams;
	console.log(path, params);
  namee = params.get("name");
  passworde = params.get("password");
  uE = params.get("Userexsists");
  res.setHeader("Access-Control-Allow-Origin", "*");
  console.log("Does the user exsist? "+uE);
  
  console.log(namee, passworde);
  insertData();
  
  // res.write("Does this user exsist: "+userExsists)
  res.write(userExsists);
	res.end();
});
server.listen(8572);


const mongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://Bluestar3211231:[email protected]/test?retryWrites=true&w=majority";
const client = new mongoClient(uri, { useNewUrlParser: true });
const assert = require('assert');


function insertData(){
 var info = {name:namee, password:passworde};

  client.connect(err => {
    const collection = client.db("CLD").collection("ChattrDatabase");
    console.log("connected!");

   /* */
   
    collection.countDocuments(info).then(function(numItems) {
      console.log("Num users = " + numItems); // Use this to debug
      if(numItems >= 1){
        console.log("User exsists");
        userExsists = "truee";
        console.log(userExsists);
      } else{
        console.log("User does not exsist")
        userExsists = "falsee";
      }
      callback(numItems);
    })

    /*if(userExsists==false){
      collection.insertOne(info, function(err, res){
         console.log("User created!");
      })
    }*/
})
}
node v10.16.0