|
@@ -0,0 +1,53 @@
|
|
|
|
|
+import express, { Express } from 'express';
|
|
|
|
|
+import multiparty from 'multiparty-express';
|
|
|
|
|
+
|
|
|
|
|
+const multipartMiddleware = multiparty();
|
|
|
|
|
+
|
|
|
|
|
+export class Webserver {
|
|
|
|
|
+ private app!: Express;
|
|
|
|
|
+
|
|
|
|
|
+ constructor(port: number) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.app = express();
|
|
|
|
|
+
|
|
|
|
|
+ this.app.set('trust proxy', 1); // Must be set for SessionHandler -> cookie.secure = 'auto' to work
|
|
|
|
|
+ this.app.disable('x-powered-by'); // Prevent Express server to send Header "X-Powered-By: Express" (websecurity issue)
|
|
|
|
|
+
|
|
|
|
|
+ // Parse Bodies containing application/json
|
|
|
|
|
+ this.app.use(express.json());
|
|
|
|
|
+ // Parse Bodies containing application/x-www-form-urlencoded
|
|
|
|
|
+ this.app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
+ // Parse Bodies containing multipart/form-data
|
|
|
|
|
+ this.app.use((req, res, next) => {
|
|
|
|
|
+ if (req.header('content-type')?.startsWith('multipart/')) {
|
|
|
|
|
+ multipartMiddleware(req, res, next);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ next();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ /** Send any request to /echo - receive your request data back */
|
|
|
|
|
+ this.app.use('/echo', (req, res, next) => {
|
|
|
|
|
+ res.send({
|
|
|
|
|
+ method: req.method,
|
|
|
|
|
+ protocol: req.protocol,
|
|
|
|
|
+ hostname: req.hostname,
|
|
|
|
|
+ url: req.url,
|
|
|
|
|
+ originalUrl: req.originalUrl,
|
|
|
|
|
+ query: req.query,
|
|
|
|
|
+ headers: req.headers,
|
|
|
|
|
+ cookies: req.cookies,
|
|
|
|
|
+ body: req.body,
|
|
|
|
|
+ fields: req.fields
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ this.app.listen(port, () => {
|
|
|
|
|
+ console.log(`Example app listening on port ${port}`);
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error(error);
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|