Created
September 13, 2016 23:32
-
-
Save gevorg/36362847c559e627ec26f5680a924976 to your computer and use it in GitHub Desktop.
Opening specific path with http-auth and http server.
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
// HTTP module | |
var http = require('http'); | |
// Authentication module. | |
var auth = require('http-auth'); | |
var basic = auth.basic({ | |
realm: "Simon Area.", | |
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass | |
}); | |
// Creating new HTTP server. | |
http.createServer(function(req, res) { | |
if (req.url.startsWith('/specific/path')) { | |
// Open for connections | |
res.end("Public path!"); | |
} else { | |
(auth.connect(basic))(req, res, function() { | |
res.end("Welcome to private area - " + req.user + "!"); | |
}); | |
} | |
}).listen(1337, function () { | |
// Log URL. | |
console.log("Server running at http://127.0.0.1:1337/"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment