-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
66 lines (53 loc) · 1.42 KB
/
server.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
const express = require("express");
let bodyParser = require("body-parser");
const { startDatabase } = require("./database");
let cors = require("cors");
const app = express();
const port = process.env.PORT || 3000;
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.use(cors());
const dbSetup = async (req, res, next) => {
if (!req.db) {
const db = await startDatabase();
req.db = db;
}
next();
};
app.use(dbSetup);
app.get("/", (req, res) => {
res.send("Welcome to the Awesome Users API!");
});
app.get("/users/get", async (req, res) => {
const users = await req.db.collection("users").find().toArray();
res.status(200).send(users);
});
app.post("/users/create", async (req, res) => {
try {
//Check for existing user
const users = await req.db
.collection("users")
.find({
name: req.body.name
})
.toArray();
if (users.length === 0) {
const save_sub = await req.db.collection("users").insertOne(req.body);
res.status(201).send({
message: "User successfully registered"
});
} else {
res.status(401).send({
message: "User already registered"
});
}
} catch (error) {
console.log(error);
res.status(500).send(error);
}
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});