-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (95 loc) · 2.89 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const app = express();
const rateLimit = require('express-rate-limit')
const bcrypt = require("bcryptjs");
app.use(express.urlencoded({extended: true}));
app.use(express.json());
require('dotenv').config({path: "./index.env"});
const ContactModel = require("./modals/contact");
const UserModel = require("./modals/users");
const PORT=process.env.PORT1 || process.env.PORT2;
const URI= process.env.MONGPDB_URI;
app.listen(PORT, () => console.log(`server running on port ${PORT}...`));
mongoose.connect(`${URI}`);
app.use(cors());
var bodyParser = require('body-parser');
app.use(bodyParser.json());
const jwt = require("jsonwebtoken")
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
// Apply the rate limiting middleware to all requests
app.use(limiter)
app.post("/Hospitauxpub", async (req, res) => {
try {
const collectionName = 'Hospitauxpub';
const collection = mongoose.connection.db.collection(collectionName);
let verify = await collection.find().toArray();
res.send(verify);
} catch (err) {
console.log(err);
}
});
app.post("/Signin",async (req,res)=>{
const users = req.body;
const password= req.body.password
try {
let verify = await UserModel.findOne({
email: users.email,
});
if (verify) {
const auth = await bcrypt.compare(password, users.password)
if (!auth) {
return res.send({message:'Incorrect password or email' })
}
res.send(verify);
}
else {
res.send(verify);
}
}
catch (err) {
console.log(err);
}
});
app.post("/Signup", async (req, res) => {
const users = req.body;
try {
let verify = await UserModel.findOne({
email: users.email
});
if (verify) {
console.log("user already exist")
res.send(verify);
} else {
const newuser = new UserModel(users);
let save = await newuser.save();
if (save) {
res.send(save);
} else {
res.send("not inserted");
}
}
} catch (err) {
console.log(err);
}
});
app.post("/Contact", async (req, res) => {
const contact = req.body;
try {
const newcontact = new ContactModel(contact);
let save = await newcontact.save();
if (save) {
res.send("newcontact inserted");
} else {
res.send("not inserted");
}
} catch (err) {
console.log(err);
}
});