[Node.js] How to set up Basic authentication in Express.js

Tadashi Shigeoka ·  Wed, November 28, 2012

I’ll introduce how to set up Basic authentication in Express.js.

Express.js

Basic Authentication for the Entire Application

app.use(express.basicAuth('username', 'password'));

or

app.use(express.basicAuth(function(user, password) {
  return user === 'username' && password === 'password';
}));

Basic Authentication for Specific Routing

app.all('/admin/*', express.basicAuth(function(user, password) {
  return user === 'username' && password === 'password';
}));

That’s all from the Gemba.

Reference Information