This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileServer.js
61 lines (50 loc) · 1.57 KB
/
fileServer.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
const { findFreePort } = require("./utils");
const http = require("http");
const fs = require("fs");
async function serveFile(file, port) {
if (!port) port = await findFreePort();
//serve file, shutdown server after first hit
const server = http.createServer(async (req, res) => {
res.writeHead(200, { "Content-Type": "application/octet-stream" });
const readStream = fs.createReadStream(file);
readStream.pipe(res);
readStream.on("end", () => {
server.close();
});
});
server.listen(port);
return port;
}
async function startRecieve(location) {
const port = await findFreePort();
//run http server that listens for POST requests. When a request is received, the server will save the file to the specified location
const server = http.createServer(async (req, res) => {
if (req.method === "POST") {
const writeStream = fs.createWriteStream(location);
//pipe req body to file
req.pipe(writeStream);
req.on("end", () => {
res.writeHead(200);
res.end("file received");
});
writeStream.on("finish", () => {
server.close();
});
writeStream.on("error", (err) => {
console.error(err);
server.close();
});
} else {
res.writeHead(405);
res.end(
"You have stumbled upon the DockGuard uploading server. This server is used to pass the export file to DockGuard. This server should shut down automatically after the file is received."
);
}
});
server.listen(port);
return port;
}
module.exports = {
startRecieve,
serveFile,
};