copy and past this code into your cli and run it with your mongo connection string
It will be your server code for mongo data base connection. Save it as mongo.js
var express =require("express");
var app=express();
var http = require('http');
const fs = require('fs')
var name=new String();
var name2=new String();
var email1=new String();
var pass=new String();
let swi=false;
let person={};
let coll1;
const path = require('node:path');
const notes = '/Users/Admin/Documents/Webpages/Rehab/Register.js';
path.dirname(notes); // /users/joe
path.basename(notes); // notes.txt
path.extname(notes);//.txt
//console.log(path.dirname(notes));
app.listen(3232);
app.use(express.json());
app.get("/Register",(req,res)=>{
res.sendFile("Register.html",{root:path.dirname(notes)});
})
// name1:document.getElementById("Name").value,
// name2:document.getElementById("last").value,
// email:document.getElementById("email").value,
// password:document.getElementById("p1").value
//var name=req.body.name1;
//var name2=req.body.name2;
// var email1=req.body.email;
// var pass=req.body.psswrd;
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "put your uri string here";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function idchk(person)
{
let eva=new String();
//console.log(findID);
if(swi==true)
{
try
{
let findID=await coll1.findOne({Email:person.email});
//let findID=await coll1.findOne({email:person.email});
//console.log(findID);
eva=findID;
return eva;
}
catch(err){console.log(err)}
}
else{console.log("error in idck swi=false");}
}
try
{
// Connect the client to the server (optional starting in v4.7)
try
{
client.connect();//conneted to the server
swi=true;
}
catch(err)
{
swi=false;
}
if(swi==true)
{
client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const db=client.db("Rehab");
const coll=db.collection("Support");
coll1=coll;
// Send a ping to confirm a successful connection
let front=false;
//let result=coll.insertOne({data:"Hello dudes"});
let data=new String();
/*async function insert()
{
let result=await coll.insertOne({person});
}
async function find(data)
{
let find1=await coll.findOne({Email:"[email protected]"});
console.log(find1);
return find1
}
console.log(find());*/
/*try
{
let find=coll.findOne({});
}
catch(err)
{
console.log(err);
}*/
}
}
catch(err)
{
console.log(err)
}
//client.close();
app.post("/Data",(req,res) =>//function here somewhere
{
person.fname=req.body.name1;
person.lname=req.body.name2;
person.email=req.body.email;
person.passwrd=req.body.psswrd;;
let s=false;
async function insert()
{
let x=false;
let idX=await idchk(person);
console.log(idX);
if(idX==null)
{
if(swi==true)
{
try
{
let result=coll1.insertOne({"Name":person.fname,"LastName":person.lname,"Email":person.email,"Password":person.passwrd});
x=true;
console.log("Account created");
}
catch(err){x=false;console.log(err);}
}
else{console.log("error in insert swi=false");}
}
else{console.log("Account already exists");}
//client.close();
}
insert();
});
Save this in the same folder as Register.html and open Local host port 3232 on the browser tab.
<!DOCTYPE html>
<html>
<head><title>Registration Page</title></head>
<body id="bdy">
<h3>Please enter the following details</h3><br><br>
<div class="stdF" id="stdFi">
<label>First Name<input type="text" id="Name"/></label><label id="in"></label><br>
<label>Last Name<input type="text" id="last"/></label><label id="in1"></label><br>
<label>Email<input type="text" id="email"/></label><label id="in2"></label><br>
<label>Password<input type="text" id="p1"/></label><label id="in3"></label><br>
<label>Confirm Password<input type="text" id="p2"/><label id="in4"></label></label><br><br>
<input type="button" onclick="home()" value="Return" class="setBut"></input>
<input type="button" onclick="send()" value="Proceed" class="setBut"></input><br><br>
</div>
<label>5 letters or more = low security<br>1 upper case, 1 lower case and 1 number = medium<br>"@","#","?","/","_","+","=","-" one = high</label>
<style>
body
{
}
.setBut
{
border-radius:6px;
font-size:30px;
background-color:rgba(200,200,100,1);
}
#stdFi
{
font-family:"Lucida Console", "Courier New", monospace;
font-size:20px;
border-style:solid;
}
</style>
<!-- Only for multipart/form-data
<input type="file" name="file">
-->
<script>
let i=0;
async function backcolor()
{
i=i+5;
if(i==255)
{
i=0;
}
document.getElementById("bdy").style.backgroundColor="rgb("+i.toString()+",200,200);";
console.log("rgb("+i.toString()+",200,200);");
//location.reload();
}
//setInterval(backcolor,1000);
/*window.alert(death);
let x=document.getElementById("stdFi");
const computedStyle = window.getComputedStyle(x);
const width = computedStyle.getPropertyValue("width");
let y=window.innerHeight;
window.alert(width+" "+y);*/
//padding-left:20px;
//text-align:center;
//let d=computedStyle.getPropertyValue("width")/2;
//computedStyle.getPropertyValue("padding-right")=d.toStrong().concat("px");
var gate=false;
var key=[false,false,false,false,false,false,false,];
//var gaten=0;
let click=0;
var cha=["@","#","?","/","_","+","=","-"];
var nu=["0","1","2","3","4","5","6","7","8","9"];
var alp=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
let data=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'];
function send()
{
gate=false;
key=[false,false,false,false,false,false,false,];
var y=emailChk(y);
//var fn1=new Strin();
//console.log(y+" sen");
//console.log("Hello");
var fn=firstNameChk(fn);
var a=lastNameChk(a);
passwordChk();
var x=[key[4],key[5],key[6]];
var y1="";
//console.log(x+" "+y+" "+z+" "+a);
//console.log(key);
//console.log(xem+" "+xname+" "+xname2);
//window.alert(xem+" "+xname+" "+xname2);
var off=false;
//var gate=false;
if(key[0]!=true || key[1]!=true)
{
//*err*-> error name has @ symbol
//err-all error in all
//err-(%) error
if(y.indexOf("err-all")!=-1)
{
y=y.slice(0,y.indexOf("err-all"));
document.getElementById("email").value=y.concat(" Invalid email");
/*const para = document.createElement("label");
const node = document.createTextNode("Invalid email");
para.appendChild(node);
const element = document.getElementById("div1");
const child = document.getElementById("p1");
element.insertBefore(para, child);*/
}
if(y.indexOf("*eer*->")!=-1)
{
//console.log(y);
y=y.slice(7,y.indexOf("@"));
document.getElementById("email").value=y.concat(" Name is incorrect");
}
if(y.indexOf("-err-")!=-1)
{
y=y.slice(y.indexOf("@"),y.indexOf("-err-"));
document.getElementById("email").value=y.concat(" Check this part");
}
//emailChk(y);
}
else
{
}
if(key[2]!=true)
{
document.getElementById("Name").value=fn+" Incorrect firstName";
}
else
{
}
if(key[3]!=true)
{
document.getElementById("last").value=a+" Incorrect lastName";
}
else
{
}
let chk=false;
let set=new String();
if(document.getElementById("p1").value!=document.getElementById("p2").value)
{
//document.getElementById("in3").value="Your passwords dont match";
document.getElementById("p2").value="Your passwords dont match";
//passwordChk();
}
else
{
if(key[4]==true)
{
if(key[5]!=true && key[6]!=true)
{
set=prompt("Your password has a low strength, click ok to keep it",p1.value);
}
if(key[6]!=true && key[5]==true)
{
set=prompt("Your password has a medium strength, click ok to keep it",p1.value);
}
if(key[6]==true)
{
}
if(set!=p1)
{
document.getElementById("p1").value=set;
document.getElementById("p2").value=set;
passwordChk();
}
}
else
{
if(document.getElementById("p1").value=="" && document.getElementById("p2").value=="")
{
document.getElementById("p1").value="Please enter an email address";
document.getElementById("p2").value="Please enter an email address";
}
else
{
document.getElementById("p1").value="Your password is to weak";
document.getElementById("p2").value="Your password is to weak";
}
}
//set chk here to confirm security level
}
console.log(key);
if(key[0]==true && key[1]==true && key[2]==true && key[3]==true && key[4]==true)
{
gate=true;
}
else{gate=false;
window.alert("Server not entered");
}
if(gate==true)
{
//off=true;
var obj=
{
name1:document.getElementById("Name").value,
name2:document.getElementById("last").value,
email:document.getElementById("email").value,
psswrd:document.getElementById("p1").value
};
click++;
if(click==1)
{
/*let request = async () =>
{
const response = await fetch('http://localhost:3232/index1.js');
const data = await response.json();
dataName = data.name;
}
let name = document.getElementById('name');
name.textContent = dataName;*/
let l=new String();
try
{
l=fetch('http://localhost:3232/index1.js');
console.log(l);
}
catch{console.log("error");}
}
//window.alert(obj);
fetch("/Data",
{
method:"POST",
headers:
{
"Content-type":"application/json"
},
body:JSON.stringify(obj)
});
window.alert("You have succefully enterd the server");
}
//window.alert("Script working");
}
function emailChk(error) //function to call
{
//error="";
var gmail=["@gmail.com","@yahoo.co.za"];
var email=document.getElementById("email").value;
// error=email;
// window.alert(email);
let em=new Array();
let email1=new String();
key[0]=false;
key[1]=false;
let fdata=["-","-"];
let emdata=new String();
let emgate=false;
let not=0;
let arrSet=0;
//console.log(gmail.length);
for(let a=0;a<gmail.length;a++)
{
em[a]=email.indexOf(gmail[a]);
//console.log(em[a]);
if(em[a]==-1)
{
not++;
//console.log(not);
}
else
{
emdata=gmail[a];
//key[0]=true;
}
}
//console.log(not+" not "+gmail.length);
if(not==gmail.length)
{
key[0]=false;
let atchk=email.indexOf("@");
if(atchk==-1)
{
emgate=false;
//fdata[1]=email.concat("err(%)");
//error=fdata[1];
}
else
{
//emgate=true;
fdata[1]=email.slice(atchk);
fdata[1]=fdata[1].concat("-err-");
}
}
else
{
emgate=true;
key[0]=true;
fdata[1]=emdata;
//console.log(emdata);
}
let atchk=fdata.indexOf("eer(%)");
let len=0;
let nlen=0;
let setN=email.indexOf("@");
if(setN!=-1)
{
let setN=email.indexOf("@");
let n=email.slice(0,setN);
//console.log("In the if condition");
nlen=n.length;
let i2=0;
for(i=0; i<nlen;i++)
{
let d=n.substring(i,i+1);
d=d.toLowerCase();
let r1=false;
//console.log("!!!!!!!!!!!!!!!!!!!!!! "+d);
let s=false;
for(let r=0;r<alp.length;r++)
{
//console.log(alp[r]+" <><><><> "+d);
let s=false;
if(d==alp[r] || d==".")
{
s=true;
r=26;
//console.log(r+" f");
if(nu[r]==d && r<10)
{
len++;
//r=alp.length;
}
else
{
len++;
//r=alp.length;
}
//r=data.length;
}
else
{
//console.log(r+" abc "+alp.length);
let q=alp.length;
q=q-1;
//console.log(r+" abc "+q);
if(r==25 && s!=true)
{
//console.log("AAAAAAAA");
key[1]=false;
if(i2==0)
{
fdata[0]="*eer*->";
fdata[0]=fdata[0].concat(d);
}
else
{
fdata[0]=fdata[0].concat(d);
}
i2++;
//console.log(fdata[0]);
}
}
}
}
}
else{key[1]=false;key[0]=false;} //console.log(len+" JK "+nlen);
//console.log(len+" Herer "+nlen);
if(len==nlen && len!=0)
{
key[1]=true;
let setN=email.indexOf("@");
let n=email.slice(0,setN);
fdata[0]=n;
emgate=true;
//console.log(len+" JK "+nlen+" "+key[1]+" we");
}
else{key[1]=false;}
if(key[1]==true && key[0]==true)
{
error=email;
//console.log("1234");
}
else if(key[0]==false && key[1]==true || key[0]==true && key[1]==false)
{
error=fdata[0].concat(fdata[1]);
}
if(key[1]==false && key[0]==false)
{
error=email.concat("err-all");
}
//error="12";
//console.log(key[0]+" "+key[1]);
return error;
//*err*-> error name has @ symbol
//err-all error in all
//err-(%) error
/*var y=0;
let i=0;
var len=0;
let gm=[false,false];
if(em!=-1)
{
y=1;
gm[0]=false;
key[0]=true;
var email1=email.slice(0,em);
console.log(email1);
var emw=email1.length;
//len=emw;
}
if(em1!=-1)
{
key[0]=true;
gm[1]=true;
var email1=email.slice(0,em1);
var emw=email1.length;
//len=emw;
for(let i=0; i<em1;i++)
{
var d=email1.substring(i,i+1);
d=d.toLowerCase();
for(let r=0;r<data.length;r++)
{
if(data[r]==d)
{
len++;
}
else
{
if(r==data.length-1)
{
if(i==0)
{
error=d;
}
else{error=error.concat(d);}
}
}
}
}
}
console.log(len+" "+i);
if(key[1]==false)
{
error=email.concat("*");
return error;
}
else
{
if(key[0]==true)
{
error=email.concat("*");
return error;
}
}
//console.log(error);
if(key[0]==false)
{
var at=email.indexOf("@");
if(at!=-1)
{
error=email.slice(at);
error=error.concat("+@");
}
else{error=email.concat("-@";}
return error;
}
else
{
error=email;
return error;
}*/
return error;
}
function firstNameChk(error)
{
//window.alert(g1);
error=document.getElementById("Name").value;
//console.log(error+" 1");
//window.alert(fname);
var name1=new String();
let f1=error.toLowerCase();
let n11=f1.length;
var first=false;
//var error="";
let f=0;
let c=0;
//var n2=new String();
for(let r=0;r<n11;r++)
{
let n1=f1.substring(r,r+1);
//window.alert(n1+" n1 data");
for(let o=0;o<26;o++)
{
//window.alert("1st loop "+i+" 2nd loop "+y+" 3rd loop "+i1+" 4th loop "+o);
//window.alert(data[o]+" "+n1);
if(n1==data[o])
{
//first=true;
key[2]=true;
o=26;
}
else
{
//console.log(n1);
//key[2]=false;
if(o==25)
{
//first=false;
//key[2]=false;
//xname=xname.concat(n1);
if(r==0)
{
c++
error="";
error=n1;
}
else
{
c++;
//console.log(n1);
if(error==document.getElementById("Name").value)
{
error="";
}
error=error.concat(n1);
}
}
}
}
}
if(c>0)
{
key[2]=false;
}
//else{error=
//console.log(error+" 2");
return error;
}
function lastNameChk(error)
{
//move variables from first name
error=document.getElementById("last").value;
//window.alert(lname);
let f2=error.toLowerCase();
var name2=new String();
var last =false;
let n22=f2.length;
var c=0;
for(let r=0;r<n22;r++)
{
let n2=f2.substring(r,r+1);
//window.alert(n1+" n1 data");
for(let o=0;o<26;o++)
{
if(n2==data[o])
{
//last=true;
key[3]=true;
o=26;
}
else
{
//key[3]=false;
if(o==25)
{
if(r==0)
{
error=n2;
c++;
}
else
{
if(error==document.getElementById("last").value)
{
error="";
}
last=false;
c++;
//xname2=xname2.concat(n1);
error=error.concat(n2);
}
}
}
}
}
if(c>0)
{
key[3]=false;
}
//console.log("error last "+key[3]);
return error;
}
function passwordChk()
{
var pass1=document.getElementById("p1").value;
var pass2=document.getElementById("p2").value;
var low=false;//length >=5
var med=false;//numbers
var high=false;//combo of length and chars +2 or more chars
var p1=pass1.length;
var a1=alp.length;
var up=0;
var down=0;
var c=cha.length;
var a=nu.length;
var dc=0;
let ca=0;
if(pass1==pass2)
{
//window.alert("Your passwords match");
for(let i=0; i<p1; i++)
{
let dw=pass1.substring(i,i+1);
for(let q=0;q<a1;q++)
{
//window.alert(dw+" "+alp[q]);
let upl=alp[q].toUpperCase();
alp[q]=alp[q].toLowerCase();
if(dw==alp[q])
{
down++;
//window.alert(down+" Lower letters");
}
//alp[q]=alp[q].toUpperCase();
if(dw==upl)
{
up++;
}
//alp[q]=alp[q].toLowerCase();
//dc=down+up;
if(i>=4)
{
//low=true;
key[4]=true;
//pspass=true;
}
else{key[4]=false;}
}
}
let dw1=0;
for(let a=0; a<pass1.length;a++)
{
let r=pass1.substring(i,i+1);
for(let q=0; q<nu.length; q++)
{
console.log(r+" "+nu[q]);
if(r==nu[q])
{
dw1++;
}
}
}
for(let q=0; q<a; q++)
{
console.log(up+" up "+down+" down nu:"+nu[q]+" dw:"+dw1);
if(key[4]==true && dw1>0)
{
//med=true;
//window.alert("medium");
if(up>0 || down>0)
key[5]=true;
//gate[1]=true;
//pspass=true;
}
else{}//set display data here
}
for(let w=0; w<pass1.length;w++)
{
let dw=pass1.substring(w,w+1);
for(let q=0; q<cha.length; q++)
{
if(cha[q]==dw)
{
ca++;
console.log(cha[q]+" "+dw);
}
else{}
}
}
if(ca>=1 && key[4]==true && key[5]==true)
{
//high=true;
if(up>0 && down>0)
key[6]=true;
//pspass=true;
}
}
else
{
console.log("else set keys false");
key[4]=false;
key[5]=false;
key[6]=false;
}
//var pssword=[low,med,high];
//return error;
}
</script>
</body>
</html>
Create a free trial account on mongodb and use the uri string they give you to add into the server code.
This is a project im busy with. Would not say no to help with CSS