Last active
September 22, 2023 09:07
-
-
Save gevorg/7168d5f02c1ca5362b2a to your computer and use it in GitHub Desktop.
Exclude specific path from http-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Express module. | |
var express = require('express'); | |
// Authentication module. | |
var auth = require('http-auth'); | |
var basic = auth.basic({ | |
realm: "Simon Area.", | |
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ... | |
}); | |
// Application setup. | |
var app = express(); | |
app.use(function(req, res, next) { | |
if ('/specific/path' === req.path) { | |
next(); | |
} else { | |
(auth.connect(basic))(req, res, next); | |
} | |
}); | |
// Setup route. | |
app.get('/', function(req, res){ | |
res.send("Hello from express - " + req.user + "!"); | |
}); | |
// Setup guest route. | |
app.get('/specific/path', function(req, res){ | |
res.send("Hello from express - guest!"); | |
}); | |
// Start server. | |
app.listen(1337); | |
// 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
This is also working