-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.js
More file actions
78 lines (58 loc) · 1.42 KB
/
exec.js
File metadata and controls
78 lines (58 loc) · 1.42 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Use Express for serving the webpages
const express = require("express");
// To write to file
const fs = require('fs');
const { get } = require("http");
// Running a file
const { exec } = require('child_process');
// Initialisation
const app = express();
const port = 3000;
// Save functions and IDs in memory
var code = {}
// Use the EJS display engine
app.set("view engine", "ejs");
//body parser
app.use(
express.urlencoded({
extended: true,
})
);
// Home page
app.get("/", (req, res) => {
res.render("home");
});
// Execute a function
app.get('/function/:id?', function(req , res){
var output = ""
exec(`node ./functions/${req.params.id}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
res.json({ output: stdout });
});
});
// Show functions
app.get('/functions', (req,res) => {
res.render("table", {functions: code});
})
// Create new function
app.post("/post_function", (req, res) => {
if (!req.body.code) {
res.status(404, "Error. No code found.")
}
var random_id = (Math.random() + 1).toString(36).substring(7);
fs.writeFile(`./functions/${random_id}`, req.body.code, err => {
if (err) {
res.status(404, "Error. No code found.")
}
// file written successfully
});
code[random_id] = req.body.code;
res.redirect('/functions')
});
// Start serving
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});