-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathadmin.js
More file actions
39 lines (30 loc) · 942 Bytes
/
admin.js
File metadata and controls
39 lines (30 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// based on https://github.com/senchalabs/connect/blob/master/examples/basicAuth.js
var basic = require("basic")
var Router = require("routes-router")
var http = require("http")
function sendBasicError(req, res) {
res.statusCode = 401
res.setHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"")
res.end("Unauthorized")
}
var auth = basic(function (user, pass, callback) {
if (user === "tj" && pass === "tobi") {
return callback(null)
}
callback(new Error("Access Denied"))
})
var app = Router()
app.addRoute("/admin", function (req, res) {
auth(req, res, function (err) {
if (err) {
return sendBasicError(req, res)
}
res.end("authorized!")
})
})
app.addRoute("*", function (req, res) {
res.end("hello! try /admin")
})
var adminServer = http.createServer(app)
adminServer.listen(3000)
console.log("basic authed admin server listening on port 3001")