diff --git a/bin/server b/bin/server
index 6cc7dba..cb0c524 100755
--- a/bin/server
+++ b/bin/server
@@ -11,6 +11,7 @@ if (argv.directory) options.directory = argv.directory;
if (argv.nocolors) options.colors = false;
if (argv.nologs) options.nologs = true;
if (argv.help) options.help = true;
+if (argv.auth || argv.a) options.auth = argv.auth || argv.a;
// Run the Server!
var url = require('../lib/server').run(options);
diff --git a/lib/login.html b/lib/login.html
new file mode 100644
index 0000000..afdc8d5
--- /dev/null
+++ b/lib/login.html
@@ -0,0 +1,39 @@
+
+
+
+ Simple HTTP Server Login
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/lib/server.js b/lib/server.js
index 3c13449..76d72f3 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -16,7 +16,8 @@
http: require('http'),
url: require('url'),
fs: require('fs'),
- path: require('path')
+ path: require('path'),
+ qs: require('querystring')
};
config = exports.config = {
@@ -64,7 +65,7 @@
};
return mimeTypes[ext.toLowerCase()];
- }
+ };
BANNER = 'Usage: nserver [options]\n\nIf called without options, `nserver` will listen to the current directory on port ' + config.port + '.';
@@ -74,7 +75,8 @@
['', '--launch', 'launch the server in your default browser after starting'],
['', '--nocolors', 'disable colors, default: false'],
['', '--nologs', 'disable request logs, default: false'],
- ['', '--help', 'show this help screen']
+ ['', '--help', 'show this help screen'],
+ ['-a', '--auth', 'allow a login screen']
];
/**
@@ -86,8 +88,8 @@
*
* @author Andrew Thorp
*/
+ var isLoggedIn = false;
exports.run = function(options){
-
// Options
if (options !== undefined){
if (options.help !== undefined) return usage();
@@ -95,6 +97,7 @@
if (options.directory !== undefined) exports.config.directory = config.directory = options.directory;
if (options.colors !== undefined) exports.config.colors = config.colors = options.colors;
if (options.nologs !== undefined) exports.config.nologs = config.nologs = options.nologs;
+ if ( options.auth ) exports.config.auth = config.auth = options.auth;
}
// Fire off the server!
@@ -110,6 +113,41 @@
var fullPath = libs.path.join(config.directory, path);
+ if ( config.auth && /auth\/?/.test( request.url ) && request.method == 'POST' ) {
+ var body = '';
+ request.on('data', function (data) {
+ body += data;
+ });
+ request.on('end',function(){
+ var POST = libs.qs.parse(body);
+
+ isLoggedIn = options.auth === POST.password;
+
+ response.writeHead(302, {
+ 'Location': '/'
+ });
+ response.end();
+ });
+ return;
+ }
+
+ if ( config.auth && !isLoggedIn ) {
+ fullPath = libs.path.join(__dirname, "login.html");
+
+ libs.fs.readFile(fullPath, function(error, data) {
+ if ( !error ){
+ response.writeHead(200, {
+ 'content-type': getMimeType( fullPath )
+ });
+ response.write(data);
+ response.end();
+ result += " - 200 AUTHENTICATE".green;
+ logString(result);
+ }
+ });
+ return;
+ }
+
libs.path.exists(fullPath, function(exists){
if (exists) {
@@ -214,7 +252,7 @@
directory: config.directory.green,
shortDir: "./".green,
url: "http://localhost:".green + config.port.toString().green + "/".green
- }
+ };
if (__dirname.indexOf(consoleStrings.directory.stripColors) !== -1){
currDir = consoleStrings.shortDir;