-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
64 lines (54 loc) · 1.63 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
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require("express");
const path = require("path");
const apiFunction = require("./twitterAPI");
const app = express();
const bodyParser = require("body-parser");
app.use(express.static(path.join(__dirname, "public")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(
express.urlencoded({
extended: true,
})
);
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
const port = process.env.PORT || 8080;
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "public/index.html"));
});
//input page to loading page
app.post("/", async function (req, res) {
let user = req.body.user.replace(/@/g, "");
res.render(path.join(__dirname, "public/loading.ejs"), {
user: user,
});
});
//loading page to results page
app.post("/results", async function (req, res) {
let user = req.body.user.replace(/@/g, "");
fetchAPIResults(user, req, res);
});
async function fetchAPIResults(user, req, res) {
var results = await apiFunction.fetchTwitterData(user);
//return error for nonexistent user
if (results == "error") {
res.render(path.join(__dirname, "/public/error.ejs"), {
results: user,
explanation: " cannot be found.",
});
}
//return error for private user
else if (results == user) {
res.render(path.join(__dirname, "/public/error.ejs"), {
results: results,
explanation: " is private.",
});
} else {
res.render(path.join(__dirname, "/public/results.ejs"), {
results: results,
});
}
}
app.get("/user", function (req, res) {});
app.listen(port);
console.log("Server started at http://localhost:" + port);