From 4fe2e60c8aaf85eaf9ecd55d71383a055854a63b Mon Sep 17 00:00:00 2001 From: Pratik Borsadiya Date: Thu, 8 Jun 2017 22:13:51 +0530 Subject: [PATCH] Add support to use Node as webserver --- README.md | 5 ++++- node-server.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 node-server.js diff --git a/README.md b/README.md index b432aea..9942055 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,10 @@ aria2c --enable-rpc --rpc-listen-all If aria2 is not installed in your local machine then head on to https://aria2.github.io/ and follow the instructions there. -Then download the webui, you can either do that by downloading this repository and running index.html in the browser. Or you could just head on to https://ziahamza.github.io/webui-aria2/ and just start downloading files! After that you can also save it for offline use by saving from the browser save page as option. +Then download the webui, you can either do that by downloading this repository and running index.html in the browser. Or you could just head on to https://ziahamza.github.io/webui-aria2/ and just start downloading files! After that you can also save it for offline use by saving from the browser save page as option. You can also use node js to create simple server by using the following command from the download folder. +````bash +node node-server.js +```` Tips ==== diff --git a/node-server.js b/node-server.js new file mode 100644 index 0000000..fa8724c --- /dev/null +++ b/node-server.js @@ -0,0 +1,36 @@ +var http = require("http"), + url = require("url"), + path = require("path"), + fs = require("fs") + port = process.argv[2] || 8888; + +http.createServer(function(request, response) { + var uri = url.parse(request.url).pathname, + filename = path.join(process.cwd(), uri); + + fs.exists(filename, function(exists) { + if(!exists) { + response.writeHead(404, {"Content-Type": "text/plain"}); + response.write("404 Not Found\n"); + response.end(); + return; + } + + if (fs.statSync(filename).isDirectory()) filename += '/index.html'; + + fs.readFile(filename, "binary", function(err, file) { + if(err) { + response.writeHead(500, {"Content-Type": "text/plain"}); + response.write(err + "\n"); + response.end(); + return; + } + response.writeHead(200); + response.write(file, "binary"); + response.end(); + }); + + }); +}).listen(parseInt(port, 10)); + +console.log("WebUI Aria2 Server is running on http://localhost:" + port );