forked from cmwylie19/lists-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (60 loc) · 1.78 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const db = require("./db");
const List = require("./list");
const port = process.env.PORT || 8080;
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (req, res, next) {
console.log("/" + req.method);
console.log("x-forwarded-for: ", req.headers["x-forwarded-for"]);
console.log("Remote address: ", req.connection.remoteAddress);
console.log("Request Path: ", req.path);
next();
});
app.post("/list", (req, res) => {
const { title, owner } = req.body;
const list = new List({ title, owner });
list.save((err, result) => (err ? res.send({ err }) : res.send({ result })));
});
app.put("/list", (req, res) => {
const { _id, allow, owner } = req.body;
if (allow !== undefined) {
List.updateOne(
{ _id },
{ $set: { allow } },
{ runValidators: true },
(err, result) => (err ? res.send({ err }) : res.send({ result }))
);
}
if (owner !== undefined) {
List.updateOne(
{ _id },
{ $set: { owner } },
{ runValidators: true },
(err, result) => (err ? res.send({ err }) : res.send({ result }))
);
}
});
app.get("/list/:id", (req, res) => {
List.find({ _id: req.params.id }, (err, result) =>
err ? res.send({ err }) : res.send({ result })
);
});
app.get("/list", (req, res) => {
List.find({}, (err, result) =>
err ? res.send({ err }) : res.send({ result })
);
});
app.delete("/list/:id", (req, res) => {
List.deleteOne({ _id: req.params.id }, (err, result) =>
err ? res.send({ err }) : res.send({ result })
);
});
app.listen(port, () => {
console.log(`list-v1 listening on ${port}!`);
});