-
Notifications
You must be signed in to change notification settings - Fork 0
/
step3.js
49 lines (42 loc) · 1.32 KB
/
step3.js
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
40
41
42
43
44
45
46
47
48
49
// Load a couple built-in node libraries
var HTTPS = require('https');
var FS = require('fs');
// Load a couple third-party node modules
var Stack = require('stack');
var Creationix = require('creationix');
// Serve files relative to the current working directory
var root = process.cwd();
// Listen on the alt-https port
var port = process.env.PORT || 8433;
// Load our self-signed cert for HTTPS support
var options = {
key: FS.readFileSync(__dirname + '/keys/nodebits-key.pem'),
cert: FS.readFileSync(__dirname + '/keys/nodebits-cert.pem')
};
// Mock data for our users database
var users = {
creationix: "sup3rS3cr3t",
guest: "noderocks"
};
// Simple access rules
var access = {
GET: {creationix: true, guest: true},
PUT: {creationix: true},
DELETE: {creationix: true}
};
// Authentation and Authorization in one go
function checker(req, username, password) {
var allowed = access[req.method];
if (allowed[username] && users[username] === password) {
return username;
}
}
// Stack up a server and start listening
HTTPS.createServer(options, Stack(
Creationix.log(),
Creationix.auth(checker, "My Sample Domain"),
Creationix.indexer("/", root),
Creationix.static("/", root)
)).listen(port);
// Give the user a nice message on the standard output
console.log("Serving %s at https://localhost:%s/", root, port);