-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (63 loc) · 1.91 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
67
68
69
70
71
72
73
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "localhost",
user: "write-something-user",
password: process.env.MYSQL_PASSWORD,
database: "write_something_development",
});
connection.connect();
const app = express();
app.use(cors());
app.use(bodyParser.json());
// Add a route to serve all the written messages (entries)
app.get("/entries", (req, res) => {
connection.query("SELECT * FROM entries;", function (error, results, fields) {
if (error) {
console.error(`ERROR: ${error.message}`);
res.send([]);
return;
}
res.send(results);
});
});
app.post("/entries", (req, res) => {
const newEntry = {
text: req.body.text,
// refactor: use ...
// todo: filter accepted properties
// accepted_properties = ['text', 'whatever']
// (make a new filter helper function.
// e.g. filterEntryProperties(req.body) )
};
// invalid cases: undefined, null, ""
if (newEntry.text == null || newEntry.text === "") {
res.status(422).send({ error: "Entry text is required." });
return;
}
else if (newEntry.text.length > 256) {
res.status(422).send({ error: "Entry text is at most 256 characters."});
return;
}
connection.query("INSERT INTO entries SET ?", newEntry, function(error, results, fields) {
if (error) {
res.status(500).send({ error: "Failed to create new entry. Please try again later." });
return;
}
newEntry.id = results.insertId;
res.send(newEntry);
});
});
const server = app.listen(3000, () => {
console.log("Server is listening on port 3000");
});
const onShutdown = () => {
console.log("Closing connection to the database.");
connection.end();
console.log("Closing the server.");
server.close();
};
process.on("SIGINT", onShutdown);
process.on("SIGTERM", onShutdown)