-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (34 loc) · 1 KB
/
index.js
File metadata and controls
41 lines (34 loc) · 1 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 5000;
// Secure token from environment variable
const SECURE_TOKEN = process.env.SECURE_TOKEN;
// Middleware
app.use(bodyParser.text({ type: "text/plain" }));
// Function to check token
const checkToken = (req, res, next) => {
const token = req.headers.authorization;
if (token === SECURE_TOKEN) {
next();
} else {
res.status(401).json({ error: "Unauthorized" });
}
};
// Execute endpoint
app.post("/execute", checkToken, (req, res) => {
const code = req.body;
if (!code) {
return res.status(400).json({ error: "No code provided" });
}
try {
// Execute the code
const result = eval(code);
res.json({ result });
} catch (error) {
res.status(500).json({ error: error.message, trace: error.stack });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});