-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
134 lines (119 loc) · 4.1 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// server.js
// where your node app starts
// init project
const express = require("express");
const bodyParser = require("body-parser");
const feedparser = require("./feedparser");
const opmlparser = require("./opmlparser");
const storage = require("./storageController")
const app = express();
const fs = require("fs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// init sqlite db
const dbFile = "./.data/sqlite.db";
const exists = fs.existsSync(dbFile);
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database(dbFile);
// Initialize our persistent storage.
storage.initialize();
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
response.sendFile(`${__dirname}/views/index.html`);
});
// A test endpoint for parsing the OPML file in the public directory.
app.get("/opml", async function(request, response) {
// Call the `getFeedsFromOPML` method in opmlparser.js
opmlparser.getFeedsFromOPML().then(data => {
const feeds = [];
// For each feed we find in the OPML file, send it to the
// feed parser and add the response to the `feeds` array.
// If there are any errors, send them back to the client.
data.forEach((feed, index) => {
feedparser.parse(feed).then(items => {
feeds.push(items);
// When we get to the last feed, send the whole feed
// array back to the client.
if (index === data.length - 1) { response.send(feeds); }
}).catch(error => {
response.send({ error: error });
});
});
}).catch(error => {
response.send({ error: error });
});
});
// API endpoint to parse an RSS/Atom feed.
app.get("/api/parse/:feed", async function(request, response) {
// Try to parse the feed, and return the result to the API client.
feedparser
.parse(request.params.feed)
.then(data => {
// Success! Return the result to the API client.
const lastFetched = new Date();
storage.insertNewestEntries(data);
response.send(data);
})
.catch(error => {
// Oh no! Return the error to the API client.
response.send(error);
});
});
// endpoint to get all the dreams in the database
app.get("/getDreams", (request, response) => {
db.all("SELECT * from Dreams", (err, rows) => {
response.send(JSON.stringify(rows));
});
});
// endpoint to add a dream to the database
app.post("/addDream", (request, response) => {
console.log(`add to dreams ${request.body.dream}`);
// DISALLOW_WRITE is an ENV variable that gets reset for new projects
// so they can write to the database
if (!process.env.DISALLOW_WRITE) {
const cleansedDream = cleanseString(request.body.dream);
db.run(`INSERT INTO Dreams (dream) VALUES (?)`, cleansedDream, error => {
if (error) {
response.send({ message: "error!" });
} else {
response.send({ message: "success" });
}
});
}
});
// endpoint to clear dreams from the database
app.get("/clearDreams", (request, response) => {
// DISALLOW_WRITE is an ENV variable that gets reset for new projects so you can write to the database
if (!process.env.DISALLOW_WRITE) {
db.each(
"SELECT * from Dreams",
(err, row) => {
console.log("row", row);
db.run(`DELETE FROM Dreams WHERE ID=?`, row.id, error => {
if (row) {
console.log(`deleted row ${row.id}`);
}
});
},
err => {
if (err) {
response.send({ message: "error!" });
} else {
response.send({ message: "success" });
}
}
);
}
});
// helper function that prevents html/css/script malice
const cleanseString = function(string) {
return string.replace(/</g, "<").replace(/>/g, ">");
};
// listen for requests :)
var listener = app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});