Skip to content

Commit

Permalink
Replace legacy server with a self-made one reading also folders
Browse files Browse the repository at this point in the history
Signed-off-by: pdulvp <pdulvp@laposte.net>
  • Loading branch information
pdulvp committed Dec 4, 2021
1 parent 60bdadb commit 694f92b
Show file tree
Hide file tree
Showing 157 changed files with 115 additions and 17,452 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# README #

This repository is a local server that can be used like:
This repository is a http local server that can be used like:
```
node.exe server.js "%FOLDER%" "%PORT%"
node.exe index.js "%FOLDER%" "%PORT%"
```

It requires [Connect](https://www.npmjs.com/package/connect) and [ServeStatic](https://github.com/expressjs/serve-static). with Node.js:
```
npm install connect serve-static
```

Binary file added icons/152.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var http = require('http');
var fs = require('fs');
var path = require('path');

var folder = process.argv[2];
if (folder == null) {
console.log('no folder specified');
return;
}

var port = process.argv[3];
if (port == null) {
console.log('no port specified');
return;
}

var contentTypes = {
js: 'text/javascript',
css: 'text/css',
woff2: 'font/woff2',
json: 'application/json',
png: 'image/png',
jpg: 'image/jpg',
ttf: 'font/ttf',
map: 'text/plain',
svg: 'image/svg+xml'
};

var defaultFile = 'index.html';

http.createServer(function (request, response) {
var filePath = folder + request.url;
filePath = filePath.split("?")[0]; //remove some ?xxx at the end of the requested file

var extname = path.extname(filePath).substring(1);
var contentType = contentTypes[extname] == undefined ? 'text/html' : contentTypes[extname];

if (filePath.indexOf("@icons") >= 0) {
filePath = filePath.substring(filePath.lastIndexOf("/"));
console.log(filePath);
fs.readFile("icons/"+filePath, function(error, content) {
console.log("[200] "+filePath);
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
return;
}

fs.stat(filePath, function(error, stats) {
if (error) {
fs.readFile('./'+error.code+'.html', function(error2, content) {
if (error2) {
console.log("[500] " + filePath);
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();

} else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});

} else if (stats.isDirectory()) {
fs.readdir(filePath, { withFileTypes: true }, (err, files) => {
if (err) {
console.log(err);
} else {
console.log("filePath");
let path = filePath.length == 1 ? "": filePath.substring(folder.length) + "";
let toLi = function(f) {
let img = f.isFile() ? "/@icons/152.png": "/@icons/4.png";
return `<li><img src="${img}" width="16" height="16" style="padding-right: 12px; "/><a href="${path}/${f.name}">${f.name}</a></li>`;
};
let resultFolders = files.filter(f => !f.isFile()).map(f => toLi(f)).join("");
let resultFiles = files.filter(f => f.isFile()).map(f => toLi(f)).join("");
res = `<html><body><h1>${path}</h1><ul>${resultFolders}</ul><ul>${resultFiles}</ul></html>`;
response.writeHead(200, { 'Content-Type': contentType });
response.end(res, 'utf-8');
}
});

} else {

fs.readFile(filePath, function(error, content) {
console.log("[200] "+filePath);
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}
});

}).listen(port);
console.log('Server running at http://127.0.0.1:'+port+'/');
15 changes: 0 additions & 15 deletions node_modules/.bin/mime

This file was deleted.

7 changes: 0 additions & 7 deletions node_modules/.bin/mime.cmd

This file was deleted.

0 comments on commit 694f92b

Please sign in to comment.