Selaa lähdekoodia

Initial Commit - Simpler echo service mit gängigen bodyparsern

Christian Kahlau 3 vuotta sitten
commit
9dec004282
8 muutettua tiedostoa jossa 134 lisäystä ja 0 poistoa
  1. 1 0
      .env
  2. 3 0
      .gitignore
  3. 10 0
      .prettierrc.js
  4. 18 0
      .vscode/settings.json
  5. 27 0
      package.json
  6. 6 0
      src/index.ts
  7. 53 0
      src/webserver.class.ts
  8. 16 0
      tsconfig.json

+ 1 - 0
.env

@@ -0,0 +1 @@
+WEB_PORT=8999

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+node_modules/
+package-lock.json
+dist/

+ 10 - 0
.prettierrc.js

@@ -0,0 +1,10 @@
+module.exports = {
+    arrowParens: 'avoid',
+    bracketSpacing: true,
+    endOfLine: 'lf',
+    printWidth: 150,
+    singleQuote: true,
+    semi: true,
+    tabWdith: 2,
+    trailingComma: "none"
+};

+ 18 - 0
.vscode/settings.json

@@ -0,0 +1,18 @@
+{
+  "editor.formatOnSave": true,
+  "editor.defaultFormatter": "esbenp.prettier-vscode",
+  "editor.tabSize": 2,
+  "typescript.format.enable": false,
+  "[typescript]": {
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
+  },
+  "html.format.enable": false,
+  "javascript.format.enable": false,
+  "json.format.enable": false,
+  "[json]": {
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
+  },
+  "[html]": {
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
+  }
+}

+ 27 - 0
package.json

@@ -0,0 +1,27 @@
+{
+  "name": "express-starter",
+  "version": "1.0.0",
+  "description": "Boilerplate Project for ExpressJS Webserver in Typescript",
+  "main": "dist/index.js",
+  "scripts": {
+    "build": "tsc -b",
+    "start": "npm run build && node ."
+  },
+  "keywords": [
+    "express",
+    "typescript",
+    "boilerplate",
+    "starter"
+  ],
+  "author": "Christian Kahlau, sirius-net, ©2022",
+  "license": "ISC",
+  "dependencies": {
+    "dotenv": "^16.0.1",
+    "express": "^4.18.1",
+    "multiparty-express": "^0.1.9"
+  },
+  "devDependencies": {
+    "@types/express": "^4.17.13",
+    "typescript": "^4.6.4"
+  }
+}

+ 6 - 0
src/index.ts

@@ -0,0 +1,6 @@
+import dotenv from 'dotenv';
+import { Webserver } from './webserver.class';
+
+dotenv.config();
+
+new Webserver(Number.parseInt(process.env.WEB_PORT));

+ 53 - 0
src/webserver.class.ts

@@ -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);
+    }
+  }
+}

+ 16 - 0
tsconfig.json

@@ -0,0 +1,16 @@
+{
+  "compilerOptions": {
+    "outDir": "./dist",
+    "esModuleInterop": true,
+    "moduleResolution": "node",
+    "module": "commonjs",
+    "target": "ES2016",
+    "baseUrl": "./src",
+    "rootDirs": ["./src"],
+    "resolveJsonModule": true,
+    "paths": {
+      "*": ["node_modules/*", "src/*"]
+    }
+  },
+  "include": ["./src/*", "./src/**/*"]
+}