greenlock-express.js/examples/cluster/server.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-11-01 10:12:40 +00:00
"use strict";
//require("greenlock-express")
require("../../")
2020-01-11 00:51:25 +00:00
.init({
packageRoot: __dirname,
configDir: "./greenlock.d",
2019-11-01 10:12:40 +00:00
2020-01-11 00:51:25 +00:00
maintainerEmail: "jon@example.com",
2019-11-01 10:12:40 +00:00
2020-01-11 00:51:25 +00:00
// When you're ready to go full cloud scale, you just change this to true:
// Note: in cluster you CANNOT use in-memory state (see below)
cluster: true,
2019-11-01 10:12:40 +00:00
2020-01-11 00:51:25 +00:00
// This will default to the number of workers being equal to
// n-1 cpus, with a minimum of 2
workers: 4
2019-11-01 21:14:07 +00:00
})
2020-01-17 00:16:30 +00:00
// ready is only executed by workers (no-op in master)
.ready(httpsWorker)
// master is only executed by master (no-op in a worker)
.master(function() {
console.info("I'm the master");
});
2019-11-01 10:12:40 +00:00
function httpsWorker(glx) {
2019-11-01 21:14:07 +00:00
// WRONG
// This won't work like you
// think because EACH worker
// has ITS OWN `count`.
var count = 0;
2019-11-01 10:12:40 +00:00
2019-11-01 21:14:07 +00:00
var app = function(req, res) {
res.end("Hello... how many times now? Oh, " + count + " times");
count += 1;
};
2019-11-01 10:12:40 +00:00
2019-11-01 21:14:07 +00:00
// Serves on 80 and 443... for each worker
// Get's SSL certificates magically!
glx.serveApp(app);
2019-11-01 10:12:40 +00:00
}