-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (51 loc) · 1.79 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
const fs = require("fs/promises");
const express = require("express");
const cors = require("cors");
const _ = require("lodash");
const { v4: uuid } = require("uuid");
const app = express();
app.use(express.json());
app.get("/outfit", (req, res) => {
const Tops = ["Black T-Shirt", "White T-Shirt", "Blue Shirt", "White Shirt"];
const Jeans = ["Black Jeans", "Blue Jeans", "Black Shorts", "Blue Shorts"];
const Shoes = ["Black Shoes", "White Shoes", "Brown Shoes", "Red Shoes"];
res.json ({
Tops: _.sample(Tops),
Jeans: _.sample(Jeans),
Shoes: _.sample(Shoes)
});
});
app.put("/outfit", async(req, res) => {
const tops = req.body.tops;
const jeans = req.body.jeans;
const shoes = req.body.shoes;
if (!tops || !jeans || !shoes) {
return res.status(400).json({ error: "Please provide a Tops, Jeans and Shoes" });
}
await fs.mkdir("data/outfit", { recursive: true });
await fs.writeFile(`data/outfit/${tops, jeans, shoes}.txt`, tops, jeans, shoes);
res.status(201);
});
app.get("/comments/:id", async (req, res) => {
const id = req.params.id;
let imput;
try {
imput = await fs.readFile(`data/comments/${id}.txt`, "utf-8");
} catch (error){
return res.status(404).json({ error: " not found"});
}
res.json({
imput: imput
});
});
app.post("/comments", async(req, res) => {
const id = uuid();
const imput = req.body.imput;
if (!imput) {
return res.status(400).json({ error: "Please provide a comment" });
}
await fs.mkdir("data/comments", { recursive: true });
await fs.writeFile(`data/comments/${id}.txt`, imput);
res.status(201);
});
app.listen(3000, () => console.log("Server is running on port 3000"));