Skip to content

Instantly share code, notes, and snippets.

@tkd55
Last active August 23, 2020 11:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkd55/43b5ad10f8c55f01d553 to your computer and use it in GitHub Desktop.
Save tkd55/43b5ad10f8c55f01d553 to your computer and use it in GitHub Desktop.
express 4.x系でBasic認証

express4.x系

4.x系からは「basic-auth-connect」をインストール

$ npm install basic-auth-connect

全体にBasic認証

var basicAuth = require('basic-auth-connect');
app.use(basicAuth('username', 'password'));

特定のRoutingにBasic認証

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

express3.x系

全体にBasic認証

app.configure(function(){
    app.use(express.basicAuth(function(user, pass){
        return 'username' == user & 'password' == pass;
    }));
    app.use(app.router); // これより前にBasic認証を書く
 });

特定のRoutingにBasic認証

app.all('/admin/*', express.basicAuth(function(user, password) {
    return user === 'username' && password === 'password';
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment