Instantly share code, notes, and snippets.

@tkd55 /express4.x.md
Last active Mar 17, 2017

Embed
What would you like to do?
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