-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (40 loc) · 1.31 KB
/
server.js
File metadata and controls
51 lines (40 loc) · 1.31 KB
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
const bodyParser = require("body-parser");
const chalk = require("chalk");
const express = require("express");
const MongoConnector = require("./lib/MongoConnector");
const PORT = 3005;
const app = express();
const mongoClient = new MongoConnector();
console.log("Server starting...");
// async
mongoClient.init();
// app.get("/", (req, res) => res.send("Hello World"));
app.use(express.static("web", { index: ["index.html", "index.htm"] }));
app.get("/toxicity", (req, res) =>
res.sendFile("web/toxicity.html", { root: __dirname + "/web" })
);
// app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.options("/data", (req, res, next) => {
// app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.sendStatus(200);
});
app.post("/data", async (req, res) => {
try {
const persona = req.body.persona;
await mongoClient.insertDocuments([{ persona }]);
res.send({ persona });
} catch (error) {
res.sendStatus(500);
}
});
app.get("/personas", async (req, res) => {
const data = await mongoClient.findDocuments();
res.contentType("application/json");
res.send(data);
});
app.listen(PORT);
console.log(`HTTP ready at ${chalk.bold.cyan(`http://localhost:${PORT}`)}`);