-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.03 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
const express = require('express')
const cors = require("cors");
const { MongoClient } = require("mongodb");
require("dotenv").config();
const authorize = require("./authorize");
const router = require("./userAuth");
const urlShort = require("./urlShort")
const app = express()
const port = process.env.PORT || 4000;
const dbUrl = process.env.DB_URL;
app.use(express.json())
app.use(cors({
origin : "*",
credentials : true
}));
app.use("/auth", router)
app.use("/urlshort", urlShort)
app.get("/", authorize, async (req, res) => {
try {
let client = await MongoClient.connect(dbUrl);
let db = client.db("Url-Shortener");
let data = await db.collection("users").find().toArray();
if (data) {
console.log(data)
res.status(200).json(data)
} else {
res.status(404).json({ message: "No Data Found" })
}
client.close();
}
catch (error) {
console.log(error)
res.status(500)
}
})
app.listen(port, () => {
console.log(`Server is running on PORT ${port}`)
})