-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
107 lines (95 loc) · 3.09 KB
/
app.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
//Importing express and all other necessary modules
const express = require("express");
const app = express();
const path = require("path");
const ejsMate = require("ejs-mate");
const methodOverride = require("method-override");
// Child process module for python
const { spawn } = require("child_process");
//Setting up the view engine and it's directory
app.engine("ejs", ejsMate);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
//All Express middleware/ Static files
app.use(express.json());
app.use(express.urlencoded({ extended: true })); //req.body is parsed as a form
app.use(methodOverride("_method")); //Setting the query for method-override
app.use(express.static(path.join(__dirname, "public"))); //It will serve our static files
// Creating global variable for src file of mp3
// Global variable to store audio src
var src = "";
// Index/Homepage Route
app.get("/", (req, res) => {
res.locals.title = "home";
res.render("home/home.ejs", { src: src });
});
// Create Route for sending audio Text to python
app.post("/", (req, res) => {
// Getting information out of Request body
var { url } = req.body.user;
const userInformation = req.body.user;
// Using spawn to call python script
const childPython = spawn("python", [
"visibility-project-python.py",
JSON.stringify(userInformation),
]);
//Execute the python script and fetch data
childPython.stdout.on("data", (data) => {
const doIt = data.toString();
// Parsing JSON to JavaScript Object
let pythonData = JSON.parse(data);
// Destructing the object and creating variables
let str = pythonData.str;
src = pythonData.src;
console.log(`total python data ${doIt}`);
// console.log(`source printed by python ${src}`);
// User if he wants to stop traversing
// src = "stop";
// Redirecting to the specified link
if (str === "stop") {
res.redirect(url);
} else {
res.redirect(str);
}
});
childPython.stderr.on("data", (data) => {
console.error(`stdError ${data}`);
});
childPython.on("close", (code) => {
console.log(`child process exited with the code: ${code}`);
});
});
// About Route
app.get("/about", (req, res) => {
res.locals.title = "about";
res.render("about/about.ejs", { src: src });
});
// How to Use Route
app.get("/howtouse", (req, res) => {
res.locals.title = "howtouse";
res.render("howtouse/howtouse.ejs", { src: src });
});
//More Routes
// motivation Route
app.get("/motivation", (req, res) => {
const { id } = req.params;
res.locals.title = "more";
res.render("more/motivation.ejs", { id: id, src: src });
});
// benificiary Route
app.get("/beneficiary", (req, res) => {
const { id } = req.params;
res.locals.title = "more";
res.render("more/beneficiary.ejs", { id: id, src: src });
});
// technology Route
app.get("/technologies", (req, res) => {
const { id } = req.params;
res.locals.title = "more";
res.render("more/technology.ejs", { id: id, src: src });
});
//Starting up server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`You are listening at PORT: ${port}`);
});