-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
82 lines (73 loc) · 2.8 KB
/
http.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
const express = require("express");
const app = express();
app.use(express.text());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const xmlparser = require("express-xml-bodyparser");
app.use(xmlparser());
const multer = require("multer");
const upload = multer();
const morgan = require("morgan");
app.use(morgan("dev"));
app.get("/get", async function (req, res) {
console.log("Invoking /get...");
req.setEncoding("utf8");
if (req.get("Authorization")) console.log(`- authorization: ${req.get("Authorization")}`);
console.log(req.query);
res.setHeader("Content-Type", "application/json");
res.send(req.query);
});
app.post("/post", async function (req, res) {
console.log("Invoking /post...");
req.setEncoding("utf8");
console.log(`- Content type: ${req.get("content-type")}`);
if (req.get("Authorization")) console.log(`- authorization: ${req.get("Authorization")}`);
console.log(req.body);
res.setHeader("Content-Type", req.get("content-type"));
res.send(req.body);
});
app.post("/post-file", upload.any(), async function (req, res) {
console.log("Invoking /file...");
req.setEncoding("utf8");
if (req.get("Authorization")) console.log(`- authorization: ${req.get("Authorization")}`);
let contentType = req.get("content-type");
console.log(`- content type: ${contentType}`);
let data = null;
if (req.files?.length > 0) {
try {
const file = req.files[0];
console.log(`- field name: ${file.fieldname}`);
console.log(`- file name: ${file.originalname}`);
console.log(`- MIME type: ${file.mimetype}`);
data = new TextDecoder().decode(file.buffer);
contentType = file.mimetype;
} catch (err) {
data = err.toString();
}
} else if ("body" in req) {
console.log(req.body);
for (let field in req.body) {
console.log(`- field name: ${field}`);
console.log(`- content type: ${contentType}`);
data = req.body[field];
break
}
}
console.log(data);
res.setHeader("Content-Type", "text/plain");
res.send(data);
});
app.use("/files", express.static("files"));
const host = process.argv[2] || "localhost";
const port = process.argv[3] || 8080;
app.listen(
port,
host,
() => {
console.log("HTTP test server started...");
console.log(` http://${host}:${port}/get | GET with QueryString`);
console.log(` http://${host}:${port}/post | POST with text/JSON/XML`);
console.log(` http://${host}:${port}/post-file | POST with multipart/file`);
console.log(` http://${host}:${port}/files | serve static files`);
}
);