Quellcode durchsuchen

Start of a poerful single file site server

Herton vor 4 Jahren
Commit
6e9f9770f6
8 geänderte Dateien mit 84 neuen und 0 gelöschten Zeilen
  1. 2 0
      .gitignore
  2. 40 0
      htmlServer.js
  3. 17 0
      package.json
  4. 18 0
      public/index.html
  5. BIN
      public/media/favicon.ico
  6. 1 0
      public/media/robots.txt
  7. 1 0
      public/media/scripts.js
  8. 5 0
      public/media/styles.css

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.DS_Store
+.vscode/

+ 40 - 0
htmlServer.js

@@ -0,0 +1,40 @@
+const http = require('http');
+const fs = require('fs');
+const port = 2222;
+const path = require('path');
+const public = path.join(__dirname, 'public');
+const htmlFile = 'public/index.html'; //Single page app file
+/* Allowed file types */
+const types = {
+    txt: 'text/plain',
+    json: 'text/json',
+    css: 'text/css',
+    gif: 'image/gif',
+    jpg: 'image/jpeg',
+    png: 'image/png',
+    svg: 'image/svg+xml',
+    ico: 'image/ico',
+    js: 'application/javascript'
+};
+/* Simple media path validation for single page app */
+function getType(path) {
+    if (path.indexOf(`media/`) == -1) return 'html';
+    for (const type in types) {
+        if (path.indexOf(`.${type}`) != -1) return type;
+    }
+    return false;
+}
+/* Server rules */
+const server = http.createServer((req, res) => {
+    const path = req.url.toLowerCase();
+    const mediaType = getType(path);
+    if (! mediaType) res.writeHead(404).end('Not found');
+    res.writeHead(200, {
+        'Content-Type': mediaType == 'html' ? 'text/html' : types[mediaType]
+    });
+    const filePath = mediaType != 'html' ?  public + path : htmlFile;
+    res.end(fs.readFileSync(filePath));
+});
+
+server.listen(port, '127.0.0.1');
+console.info('Server running on port '+port)

+ 17 - 0
package.json

@@ -0,0 +1,17 @@
+{
+  "name": "single-page-server",
+  "version": "1.0.0",
+  "description": "Single page node server with no dependencies",
+  "main": "htmlServer.js",
+  "scripts": {
+    "start": "node htmlServer.js",
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "keywords": [
+    "server",
+    "single",
+    "page"
+  ],
+  "author": "Herton",
+  "license": "MIT"
+}

+ 18 - 0
public/index.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Basic single file HTML</title>
+    <meta name="description" content="Simgle file page basic page">
+    <link rel="stylesheet" type="text/css" href="media/styles.css">
+    <script type="text/javascript" src="media/scripts.js"></script>
+</head>
+<body>
+    <div class="wrapper">
+        <img src="media/favicon.ico" alt="icon"/>
+        <p>Basic Node Single file Server Setup</p>
+    </div>
+</body>
+</html>

BIN
public/media/favicon.ico


+ 1 - 0
public/media/robots.txt

@@ -0,0 +1 @@
+User-agent: * Disallow: /media

+ 1 - 0
public/media/scripts.js

@@ -0,0 +1 @@
+console.log('Script loaded!');

+ 5 - 0
public/media/styles.css

@@ -0,0 +1,5 @@
+.wrapper {
+    max-width: 300px;
+    margin: 1rem auto;
+    text-align: center;
+}