-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (58 loc) · 2.1 KB
/
index.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
const path = require('path');
const express = require("express");
const fileUpload = require("express-fileupload");
const bodyParser = require("body-parser");
const fs = require("fs");
const chalk = require("chalk");
const moment = require("moment");
const app = express();
const { get_lan_ip } = require("./Get_LAN_IP");
const { renderPage } = require("./page");
const PORT = 9200;
const log = console.log;
const withColor = color => text => chalk[color](text);
const withNowTime = text =>
chalk["cyan"](moment().format("YYYY/MM/DD h:mm:ss A ")) + text;
// middleware
app.use(fileUpload());
app.use(bodyParser.urlencoded({ extended: false, limit: '10mb' }));
// respond with "hello world" when a GET request is made to the homepage
app.get("/", function(req, res) {
res.send(renderPage({}));
});
const dir_public = path.join(__dirname, 'public');
app.use(express.static(dir_public));
// respond with "hello world" when a GET request is made to the homepage
app.get("/UPLOADED_FILE", function(req, res) {
res.sendFile(__dirname + "/UPLOADED_FILE");
});
app.post("/upload", function(req, res) {
log(withNowTime(withColor("yellow")(req.files))); // the uploaded file object
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send("No files were uploaded.");
}
// The name of the input field (i.e. "UPLOADING_FILE") is used to retrieve the uploaded file
let UPLOADING_FILE = req.files.UPLOADING_FILE;
// Use the mv() method to place the file somewhere on your server
UPLOADING_FILE.mv("./UPLOADED_FILE", function(err) {
if (err) return res.status(500).send(err);
res.send("File uploaded!");
});
});
app.post("/pastebin", function(req, res) {
log(withNowTime(withColor("redBright")(`Requesting pastebin service`)));
const pastebin = req.body.PASTE;
fs.writeFileSync("./PASTE_BIN", pastebin);
res.send(`
<a href="${get_lan_ip()}:${PORT}">Back to home</a>
<br />
<br />
Your post: <br /><br /><pre>${pastebin}</pre>`);
});
app.listen(PORT, () => {
log(
`Example app listening on port ${withColor("yellow")(
get_lan_ip()
)}:${withColor("red")(PORT)}`
);
});