-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
65 lines (57 loc) · 2.09 KB
/
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
64
65
const express = require('express');
const app = express();
const { MongoClient } = require("mongodb");
var bodyParser = require('body-parser')
const url = "mongodb://127.0.0.1:27017/mydb";
var dbo;
MongoClient.connect(url, (err, db) => {
if (err) throw err;
dbo = db.db("test");
console.log("Connected to MongoDB");
})
app.use(require('cors')())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.post('/api/login/', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', '*');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
dbo.collection("userInfo").find({ "username": req.body.username }).toArray(function(err, result) {
if (err) throw err;
if (result == undefined)
res.send("ConnectionFailed");
else if (result.length == 0)
res.send("UserNotFound");
else if (result[0].password == req.body.password)
res.send("PasswordCorrect");
else
res.send("PasswordIncorrect");
});
});
app.post('/api/register/', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', '*');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
dbo.collection("userInfo").find({ "username": req.body.username }).toArray(function(err, result) {
if (err) throw err;
if (result == undefined)
res.send("Failed");
else if (result.length != 0)
res.send("UserExist");
else {
let newUserInfo = {
username: req.body.username,
password: req.body.password
}
dbo.collection("userInfo").insertOne(newUserInfo, function(err, result) {
if (err) throw err;
res.send("Success");
});
}
});
});
app.listen(8000, () => {
console.log('Server is running on port 8000');
});