-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace legacy server with a self-made one reading also folders
Signed-off-by: pdulvp <pdulvp@laposte.net>
- Loading branch information
Showing
157 changed files
with
115 additions
and
17,452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+'/'); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.