-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (23 loc) · 803 Bytes
/
Copy pathindex.js
File metadata and controls
25 lines (23 loc) · 803 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
var auth = require('basic-auth')
var http = require('http')
var nodeStatic = require('node-static')
module.exports = function(config) {
var file = new nodeStatic.Server(config.root, config.options)
var port = process.env.PORT || config.port
var username = config.username
var password = config.password
http.createServer(function(req, res){
var credentials = auth(req)
var unauthenticated = !credentials || credentials.name !== username || credentials.pass !== password
if (unauthenticated && process.env.NODE_ENV == 'production') {
res.writeHead(401, {
'WWW-Authenticate': 'Basic realm="' + config.realm + '"'
})
res.end()
} else {
req.addListener('end', function () {
file.serve(req, res)
}).resume()
}
}).listen(port)
}