-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
99 lines (89 loc) · 2.44 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
const express = require("express");
const app = express();
const jwt = require("jsonwebtoken");
const dotenv = require("dotenv");
dotenv.config();
app.use(express.json());
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Access-Control-Allow-Headers"
);
next();
});
// MongoDB Client Connection Setup
const MongoClient = require("mongodb").MongoClient;
const uri =
"mongodb+srv://admin:[email protected]/<dbname>?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
var db = null;
client.connect((err, client) => {
// Client returned
if (err) throw err;
db = client.db("database");
});
// Get all users
app.get("/api/users", (req, res) => {
db.collection("sample_employees")
.find({}) // Empty query to find all
.toArray(function (err, result) {
if (err) throw err;
res.status(200).json(result);
});
});
// Get one user's info by ID
app.get("/api/users/:id", (req, res) => {
id = parseInt(req.params.id);
db.collection("sample_employees").findOne({ emp_id: id }, function (
err,
result
) {
if (err) throw err;
res.status(200).json(result);
});
});
// Get one user by ID
app.get("/api/usertoken/:id", (req, res) => {
id = parseInt(req.params.id);
// Declare promise
var myPromise = (id) => {
return new Promise((resolve, reject) => {
db.collection("sample_employees").findOne({ emp_id: id }, function (
err,
result
) {
err ? reject(err) : resolve(result);
});
});
};
// Call promise
myPromise(id)
.then((response) => {
// Create user session token
const token = jwt.sign({ user: id }, process.env.ACCESS_TOKEN_SECRET);
// Check that user was found in database
if (response === undefined || response === null) {
throw "User Not Found";
}
// Return token if found
res.status(200).json({
token: token,
});
})
.catch((error) => {
console.log("error", error);
res.status(500).send(error);
});
});
// Dummy endpoint
app.get("/api", (req, res) => {
res.send("Hello World!");
});
app.listen(process.env.PORT, () => {
console.log(`App running on port ${process.env.PORT}.`);
});