-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (85 loc) · 2.83 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { MongoClient } from "mongodb";
import bodyparser from "body-parser";
import cors from "cors";
import cookieparser from "cookie-parser";
import express, { response } from "express";
import * as dotenv from "dotenv";
import bcrypt from "bcrypt";
import { ServerApiVersion } from "mongodb";
import signInRouter from './routes/signin.js';
import signOutRouter from './routes/signout.js';
import signUpRouter from './routes/signup.js';
import updateUserRouter from './routes/update-user.js';
import allEnqyirysRouter from './routes/enquirys.js';
import allUsersRouter from './routes/users.js';
dotenv.config();
export const app = express();
// const corsOptions ={
// origin:'http://localhost:3030',
// credentials:true, //access-control-allow-credentials:true
// optionSuccessStatus:200
// }
app.use(cors());
app.use(express.json())
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
app.use(cookieparser());
//set variables in .env file - error - Issue..
const PORT = process.env.PORT;
export const JWT_SECRET = process.env.JWT_SECRET;
const MONGO_URL = process.env.MONGO_URL;
export async function hashedPassword(password) {
const NO_OF_ROUNDS = 10;
const salt = await bcrypt.genSalt(NO_OF_ROUNDS);
const hashedPassword = await bcrypt.hash(password, salt);
return hashedPassword;
}
async function MongoConnect() {
const client = await new MongoClient(MONGO_URL, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
}).connect();
console.log("Mongo Connected");
return client;
}
export const client = await MongoConnect();
app.get("/", function (request, response) {
response.send("🙋♂️ Welcome to LT Backend");
});
// Use routes sign in
app.use('/', signInRouter);
// Use routes sign out
app.use('/', signOutRouter);
// Use routes sign up
app.use('/', signUpRouter);
// update user
app.use('/user', updateUserRouter);
// enquiry
app.use('/enquirys', allEnqyirysRouter);
// enquiry
app.use('/users', allUsersRouter);
// for blog editor
app.post("/editor/:id", async function (request, response) {
try {
const id = request.params.id;
const content = request.content;
let blogdb = await client.db("LT").collection("Drafts").findOne({ id });
if (blogdb) {
response
.status(200)
.send({ msg: `Draft of ${id} already present`, blogdb });
} else {
let result = await client.db("LT").collection("Drafts").insertOne({
id,
content,
});
response.status(201).send({ msg: `Draft of ${id} added`, result });
}
} catch (error) {
response.status(500).send(error);
}
});
app.listen(PORT, () => console.log(`The server started in: ${PORT} ✨✨`));