Skip to content

Instantly share code, notes, and snippets.

@gevorg
Created September 13, 2016 23:32
Show Gist options
  • Save gevorg/36362847c559e627ec26f5680a924976 to your computer and use it in GitHub Desktop.
Save gevorg/36362847c559e627ec26f5680a924976 to your computer and use it in GitHub Desktop.
Opening specific path with http-auth and http server.
// 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