I have the same problem. I managed to get one solution that may work for you, but is not suitable for me, as it interferes with the user login from angular -fullstack.
I just need a quick way to password protect the application so that only developers and interested parties can see it. https://www.npmjs.org/package/http-auth seems to do the trick.
This is due to adding http-auth to your project (npm install http-auth --save). Then you need to find the file that defines your createServer, and the code there.
If you use Express, you can do something like this
// HTTP Authentication var preAuth = require('http-auth'); var basic = preAuth.basic({ realm: "Restricted Access! Please login to proceed" }, function (username, password, callback) { callback( (username === "user" && password === "password")); } ); // Setup server var app = express(); app.use(preAuth.connect(basic)); var server = require('http').createServer(app);
If not, you can try one of the options from the http-auth documentation, for example.
// Authentication module. var auth = require('http-auth'); var basic = auth.basic({ realm: "Simon Area." }, function (username, password, callback) { // Custom authentication method. callback(username === "Tina" && password === "Bullock"); } ); // Creating new HTTP server. http.createServer(basic, function(req, res) { res.end("Welcome to private area - " + req.user + "!"); }).listen(1337);
There are also a couple of related threads here with somewhat similar approaches.
express.basicAuth expression error
Basic HTTP authentication in Node.JS?
source share